SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Class and Objects
09/04/131 VIT - SCSE
By
G.SasiKumar., M.Tech., (Ph.D).,
Assistant Professor
School of Computing Science and Engineering
VIT University
Class
A class is a mechanism for creating user-defined data types.
09/04/132 VIT - SCSE
class class_name
{
private data and functions
access-specifier:
data and functions
access-specifier:
data and functions
.
.
.
access-specifier:
data and functions
}object-list;
Access Specifier
09/04/133 VIT - SCSE
Access Specifier is one of these three C++ keywords:
1.private
2.public
3.protected
A private member within a class denotes that only members of the same class
have accessibility. The private member is inaccessible from outside the class.
Public members are accessible from outside the class.
A protected access specifier is a stage between private and public access. If
member functions defined in a class are protected, they cannot be accessed from
outside the class but can be accessed from the derived class.
09/04/134 VIT - SCSE
class student
{
int a,b;
public :
void setdata(int x,int y)
{
a=x;
b=y;
}
void outdata()
{
cout<<“A =“<<a<<endl;
cout<<“B= “<<b<<endl;
}
};
Class Objects
Accessing a Class Members
09/04/135 VIT - SCSE
objectname . datamember;
objectname . functionname(actual arguments);
Example:
S1.setdata(10,”Rajkumar”);
S1.outdata();
Class Members
09/04/136 VIT - SCSE
1. Data Member
2. Member Function
Defining a member function of a class inside its scope
Defining a member function of a class outside its scope
Syntax:
returntype classname::memberfunctions(argument1,2,..n)
{
}
Array of class object
09/04/137 VIT - SCSE
#include<iostream.h>
class student
{
private:
long int rollno;
int age;
public:
void getinfo()
{
cout<<”Roll no:”;
cin>>rollno;
cout<<”Age:”;
cin>>age;
}
void disinfo()
{
cout<<endl;
cout<<”Roll No =”<<rollno<<endl;
cout<<”Age =”<<age<<endl;
}
};
void main()
{
student s[5];
int i,n;
cout<<”How many students?”<<endl;
cin>>n;
cout<<”Enter the following Information”<<endl;
for(i=0;i<=n-1;i++)
{
int j=i;
cout<<endl;
cout<<”record=”<<j+1<<endl;
s[i].getinfo();
}
cout<<”Contents of class”<<endl;
for(i=0;i<=n-1;i++)
{
s[i].disinfo();
}
}
Pointers and classes
09/04/138 VIT - SCSE
class sample
{
int x;
float y;
public:
void getdata();
void display()
};
sample *p;
(*objectname).membername;
or
objectname->membername;
09/04/139 VIT - SCSE
#include<iostream.h>
class student
{
private:
long int rollno;
int age;
public:
void getinfo();
void disinfo();
};
void student::getinfo()
{
cout<<”Roll no:”;
cin>>rollno;
cout<<”Age:”;
cin>>age;
}
void student::disinfo()
{
cout<<endl;
cout<<”Roll No =”<<rollno<<endl;
cout<<”Age =”<<age<<endl;
}
void main()
{
student *p;
(*p).getinfo();
p->disinfo();
}
Nested Class (classes within classes)
09/04/1310 VIT - SCSE
A class declared as a member of another class is called as a
nested class.
The name of a nested class is local to the enclosing class.
Syntax:
class outer{
private:
//member
public:
//member
class inner{
private:
//member
public:
//member
};
};
09/04/1311 VIT - SCSE
class sample
{
private:
int a,b;
public:
int x,y;
class simple
{
public:
int x,y;
void put();
void display();
};
void get();
void show();
};
void sample::get()
{
cout<<”Enter a and b
values”<<endl;
cin>>a>>b;
}
void sample::show()
{
cout<<”A=”<<a<<endl;
cout<<”B=”<<b<<endl;
}
09/04/1312 VIT - SCSE
void sample::simple::put()
{
cout<<”Enter x and y
values”<<endl;
cin>>x>>y;
}
void sample::simple::display()
{
cout<<”X=”<<x<<endl;
cout<<”Y=”<<y<<endl;
}
void main()
{
sample s;
sample::simple ob;
s.get();
s.show();
ob.put();
ob.display();
getch();
}
Constructor
09/04/1313 VIT - SCSE
A constructor is a special member function for automatic
initialization of an object.
Whenever an object is created, the special member function,
i.e., the constructor will be executed automatically.
Rules for writing constructor:
1.A constructor name must be the same as that of its class
name.
2.It is declared with no return type (not even void).
3.It cannot be declared const but a constructor can be invoked
with const objects.
4.It may not be static.
5.It may not be virtual.
6.It should have public or protected access within the class.
09/04/1314 VIT - SCSE
Syntax:
class test
{
private:
--------------
--------------
protected:
--------------
--------------
public:
test(); //constructor
--------------
};
test::test()
{
--------------
----------------
}
09/04/1315 VIT - SCSE
// generation of the fibonacci series
using constructor.
#include<iostream.h>
#include<conio.h>
class test
{
unsigned long int f0,f1,fib;
public:
test()
{
f0=0;
f1=1;
fib=f0+f1;
}
void increment()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
void display()
{
cout<<fib<<‘t’;
}
};
void main()
{
test t;
for(int i=0;i<=10;i++)
{
t.display();
t.increment();
getch();
}
Types of Constructor
09/04/1316 VIT - SCSE
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
09/04/1317 VIT - SCSE
A default constructor function initializes the data
members with no arguments.
Example:
class Exforsys
{
private:
int a,b;
public:
Exforsys();
...
};
Exforsys :: Exforsys()
{
a=0;
b=0;
}
Parameterized Constructor
09/04/1318 VIT - SCSE
Constructor with arguments
Example:
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
myclass(int i, int j) {
a=i;
b=j;
}
void show() {
cout << a << " " << b;
}
};
int main()
{
myclass ob(3, 5);
ob.show();
return 0;
}
Copy Constructor
09/04/1319 VIT - SCSE
• Copy constructors are always used when the
compiler has to create a temporary object of a
class object.
• The copy constructors are used in the following
situations:
1. The initialization of an object by another object of
the same class.
General format:
classname::classname(classname &p)
{
----------
}
09/04/1320 VIT - SCSE
class test
{
int a,b;
public:
test()
{
a=2;
b=4;
}
test(test &p)
{
a=p.a;
b=p.b;
}
void display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
};
void main()
{
test t1;
t1.display();
test t2;
t2=t1;
t2.display();
getch();
}
Destructor
09/04/1321 VIT - SCSE
A destructor is a function that automatically executes when
an object is destroyed.
class username
{
private:
//members;
public:
username(); //constructor
~username(); //destructor
};
09/04/1322 VIT - SCSE
#include<iostream.h>
#include<conio.h>
class test
{
public:
int a,b;
test()
{
a=0;
b=0;
}
~test()
{
cout<<"Object Destroyed";
}
};
void main()
{
test t1;
test t2;
test t3;
getch();
}
Constructor Overloading
09/04/1323 VIT - SCSE
Constructor with different arguments
test();
test(int a);
test(int a,int b);
test(int a,int b,int c);
09/04/1324 VIT - SCSE
class sample
{
private:
int a,b,c;
public:
sample()
{
a=0;
b=0;
c=0;
}
sample(int x)
{
a=x;
b=2;
c=3;
}
sample(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
void display();
};
void sample::display()
{
cout<<"A="<<a<<endl;
cout<<"B="<<b<<endl;
cout<<"C="<<c<<endl;
}
void main()
{
sample s1;
cout<<"Contents of s1:"<<endl;
s1.display();
sample s2(10);
cout<<"Contents of s2:"<<endl;
s2.display();
sample s3(4,8,12);
cout<<"Contents of s3:"<<endl;
s3.display();
getch();
}
Static Class Members
09/04/1325 VIT - SCSE
static data member
static member function
1.Static variables are common to all the class objects.
2.Only one copy of that member is created and shared by all
the objects of that class.
3.It is visible only within the class, but the life time is the entire
program.
09/04/1326 VIT - SCSE
class sample
{
private:
static int a;
public:
void display();
};
int sample::a;
void sample::display()
{
cout<<”content of the static data
member=”;
cout<<a;
cout<<endl;
}
void main()
{
sample s;
s.display();
getch();
}
Static member function
09/04/1327 VIT - SCSE
1. A static member function can access only other
static members declared in the same class.
2. It can be called using the class name instead of
objects as follows,
class_name :: function_name;
09/04/1328 VIT - SCSE
class sample
{
private:
static int a;
public:
sample();
static void display();
};
int sample::a=5;
sample::sample()
{
++a;
}
void sample::display()
{
cout<<”Value=”<<a<<endl;
}
void main()
{
sample::display();
sample s1,s2,s3;
sample::display();
getch();
}

Weitere ähnliche Inhalte

Was ist angesagt?

Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfacesKalai Selvi
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5Sayed Ahmed
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to javaKalai Selvi
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 

Was ist angesagt? (20)

Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Class and objects
Class and objectsClass and objects
Class and objects
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Inheritance
InheritanceInheritance
Inheritance
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Inheritance
Inheritance Inheritance
Inheritance
 
OOP C++
OOP C++OOP C++
OOP C++
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to java
 
Java packages
Java packagesJava packages
Java packages
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 

Ähnlich wie 7 class objects

Ähnlich wie 7 class objects (20)

Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
lecture3.pptx
lecture3.pptxlecture3.pptx
lecture3.pptx
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
C++ classes
C++ classesC++ classes
C++ classes
 
10 inheritance
10 inheritance10 inheritance
10 inheritance
 

Mehr von Docent Education

12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initializationDocent Education
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initializationDocent Education
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functionsDocent Education
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented ProgrammingDocent Education
 

Mehr von Docent Education (14)

17 files and streams
17 files and streams17 files and streams
17 files and streams
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
13 exception handling
13 exception handling13 exception handling
13 exception handling
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
6 pointers functions
6 pointers functions6 pointers functions
6 pointers functions
 
5 array
5 array5 array
5 array
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented Programming
 
3 intro basic_elements
3 intro basic_elements3 intro basic_elements
3 intro basic_elements
 
2 Intro c++
2 Intro c++2 Intro c++
2 Intro c++
 
unit-1-intro
 unit-1-intro unit-1-intro
unit-1-intro
 

Kürzlich hochgeladen

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Kürzlich hochgeladen (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

7 class objects

  • 1. Class and Objects 09/04/131 VIT - SCSE By G.SasiKumar., M.Tech., (Ph.D)., Assistant Professor School of Computing Science and Engineering VIT University
  • 2. Class A class is a mechanism for creating user-defined data types. 09/04/132 VIT - SCSE class class_name { private data and functions access-specifier: data and functions access-specifier: data and functions . . . access-specifier: data and functions }object-list;
  • 3. Access Specifier 09/04/133 VIT - SCSE Access Specifier is one of these three C++ keywords: 1.private 2.public 3.protected A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class. Public members are accessible from outside the class. A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.
  • 4. 09/04/134 VIT - SCSE class student { int a,b; public : void setdata(int x,int y) { a=x; b=y; } void outdata() { cout<<“A =“<<a<<endl; cout<<“B= “<<b<<endl; } };
  • 5. Class Objects Accessing a Class Members 09/04/135 VIT - SCSE objectname . datamember; objectname . functionname(actual arguments); Example: S1.setdata(10,”Rajkumar”); S1.outdata();
  • 6. Class Members 09/04/136 VIT - SCSE 1. Data Member 2. Member Function Defining a member function of a class inside its scope Defining a member function of a class outside its scope Syntax: returntype classname::memberfunctions(argument1,2,..n) { }
  • 7. Array of class object 09/04/137 VIT - SCSE #include<iostream.h> class student { private: long int rollno; int age; public: void getinfo() { cout<<”Roll no:”; cin>>rollno; cout<<”Age:”; cin>>age; } void disinfo() { cout<<endl; cout<<”Roll No =”<<rollno<<endl; cout<<”Age =”<<age<<endl; } }; void main() { student s[5]; int i,n; cout<<”How many students?”<<endl; cin>>n; cout<<”Enter the following Information”<<endl; for(i=0;i<=n-1;i++) { int j=i; cout<<endl; cout<<”record=”<<j+1<<endl; s[i].getinfo(); } cout<<”Contents of class”<<endl; for(i=0;i<=n-1;i++) { s[i].disinfo(); } }
  • 8. Pointers and classes 09/04/138 VIT - SCSE class sample { int x; float y; public: void getdata(); void display() }; sample *p; (*objectname).membername; or objectname->membername;
  • 9. 09/04/139 VIT - SCSE #include<iostream.h> class student { private: long int rollno; int age; public: void getinfo(); void disinfo(); }; void student::getinfo() { cout<<”Roll no:”; cin>>rollno; cout<<”Age:”; cin>>age; } void student::disinfo() { cout<<endl; cout<<”Roll No =”<<rollno<<endl; cout<<”Age =”<<age<<endl; } void main() { student *p; (*p).getinfo(); p->disinfo(); }
  • 10. Nested Class (classes within classes) 09/04/1310 VIT - SCSE A class declared as a member of another class is called as a nested class. The name of a nested class is local to the enclosing class. Syntax: class outer{ private: //member public: //member class inner{ private: //member public: //member }; };
  • 11. 09/04/1311 VIT - SCSE class sample { private: int a,b; public: int x,y; class simple { public: int x,y; void put(); void display(); }; void get(); void show(); }; void sample::get() { cout<<”Enter a and b values”<<endl; cin>>a>>b; } void sample::show() { cout<<”A=”<<a<<endl; cout<<”B=”<<b<<endl; }
  • 12. 09/04/1312 VIT - SCSE void sample::simple::put() { cout<<”Enter x and y values”<<endl; cin>>x>>y; } void sample::simple::display() { cout<<”X=”<<x<<endl; cout<<”Y=”<<y<<endl; } void main() { sample s; sample::simple ob; s.get(); s.show(); ob.put(); ob.display(); getch(); }
  • 13. Constructor 09/04/1313 VIT - SCSE A constructor is a special member function for automatic initialization of an object. Whenever an object is created, the special member function, i.e., the constructor will be executed automatically. Rules for writing constructor: 1.A constructor name must be the same as that of its class name. 2.It is declared with no return type (not even void). 3.It cannot be declared const but a constructor can be invoked with const objects. 4.It may not be static. 5.It may not be virtual. 6.It should have public or protected access within the class.
  • 14. 09/04/1314 VIT - SCSE Syntax: class test { private: -------------- -------------- protected: -------------- -------------- public: test(); //constructor -------------- }; test::test() { -------------- ---------------- }
  • 15. 09/04/1315 VIT - SCSE // generation of the fibonacci series using constructor. #include<iostream.h> #include<conio.h> class test { unsigned long int f0,f1,fib; public: test() { f0=0; f1=1; fib=f0+f1; } void increment() { f0=f1; f1=fib; fib=f0+f1; } void display() { cout<<fib<<‘t’; } }; void main() { test t; for(int i=0;i<=10;i++) { t.display(); t.increment(); getch(); }
  • 16. Types of Constructor 09/04/1316 VIT - SCSE 1. Default Constructor 2. Parameterized Constructor 3. Copy Constructor
  • 17. Default Constructor 09/04/1317 VIT - SCSE A default constructor function initializes the data members with no arguments. Example: class Exforsys { private: int a,b; public: Exforsys(); ... }; Exforsys :: Exforsys() { a=0; b=0; }
  • 18. Parameterized Constructor 09/04/1318 VIT - SCSE Constructor with arguments Example: #include <iostream> using namespace std; class myclass { int a, b; public: myclass(int i, int j) { a=i; b=j; } void show() { cout << a << " " << b; } }; int main() { myclass ob(3, 5); ob.show(); return 0; }
  • 19. Copy Constructor 09/04/1319 VIT - SCSE • Copy constructors are always used when the compiler has to create a temporary object of a class object. • The copy constructors are used in the following situations: 1. The initialization of an object by another object of the same class. General format: classname::classname(classname &p) { ---------- }
  • 20. 09/04/1320 VIT - SCSE class test { int a,b; public: test() { a=2; b=4; } test(test &p) { a=p.a; b=p.b; } void display() { cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; } }; void main() { test t1; t1.display(); test t2; t2=t1; t2.display(); getch(); }
  • 21. Destructor 09/04/1321 VIT - SCSE A destructor is a function that automatically executes when an object is destroyed. class username { private: //members; public: username(); //constructor ~username(); //destructor };
  • 22. 09/04/1322 VIT - SCSE #include<iostream.h> #include<conio.h> class test { public: int a,b; test() { a=0; b=0; } ~test() { cout<<"Object Destroyed"; } }; void main() { test t1; test t2; test t3; getch(); }
  • 23. Constructor Overloading 09/04/1323 VIT - SCSE Constructor with different arguments test(); test(int a); test(int a,int b); test(int a,int b,int c);
  • 24. 09/04/1324 VIT - SCSE class sample { private: int a,b,c; public: sample() { a=0; b=0; c=0; } sample(int x) { a=x; b=2; c=3; } sample(int x,int y,int z) { a=x; b=y; c=z; } void display(); }; void sample::display() { cout<<"A="<<a<<endl; cout<<"B="<<b<<endl; cout<<"C="<<c<<endl; } void main() { sample s1; cout<<"Contents of s1:"<<endl; s1.display(); sample s2(10); cout<<"Contents of s2:"<<endl; s2.display(); sample s3(4,8,12); cout<<"Contents of s3:"<<endl; s3.display(); getch(); }
  • 25. Static Class Members 09/04/1325 VIT - SCSE static data member static member function 1.Static variables are common to all the class objects. 2.Only one copy of that member is created and shared by all the objects of that class. 3.It is visible only within the class, but the life time is the entire program.
  • 26. 09/04/1326 VIT - SCSE class sample { private: static int a; public: void display(); }; int sample::a; void sample::display() { cout<<”content of the static data member=”; cout<<a; cout<<endl; } void main() { sample s; s.display(); getch(); }
  • 27. Static member function 09/04/1327 VIT - SCSE 1. A static member function can access only other static members declared in the same class. 2. It can be called using the class name instead of objects as follows, class_name :: function_name;
  • 28. 09/04/1328 VIT - SCSE class sample { private: static int a; public: sample(); static void display(); }; int sample::a=5; sample::sample() { ++a; } void sample::display() { cout<<”Value=”<<a<<endl; } void main() { sample::display(); sample s1,s2,s3; sample::display(); getch(); }