SlideShare uma empresa Scribd logo
1 de 25
CLASSES AND
OBJECTS IN C++
by
Mrs. B.Arulmozhi, M.Sc., B.Ed., M.Phil., SET., NET.,
Assistant Professor and Head, Department of Computer
Science and Applications
D.K.M. College for Women, Vellore
SYNPOSIS
• Class
• Syntax
• Data Abstraction
• Example
• Creating Object
• Defining member function inside and
outside
• Memory allocation of object
• Example
CLASSES
It is a user defined data type.
A class is a way to bind the data and its
associated functions together.
Example:Class student
{
char name[30];
int rollno,m1,m2,tot;
public:void calculate();
};
Specifying class:
It has 2 parts.
1. class declaration
2. class function definitions.
Syntax:
Class class-name
{
private: variable-declaration; function-declaration;
public: variable-declaration; function-declaration;
protected: variable-declaration; function-declaration;
}
Example
class student
{
private:
char name[20];
int roll,m1,m2,tot;
protected:
void accept();
void compute();
void display();
public:
student();
void execute();
};
Rules
• class is a keyword
• Body of the class enclosed within braces and
terminated by ;
• Body contains declarations of variables and functions
• It contain 3 access specifiers.(visibility labels)
• private, public and protected
• private members accessed only with in the class.
• public members accessed outside of the class also.
• protected members accessed the members of the
inherted classes only
Data abstraction
• The binding of data and functions together into a
single entity is referred to as encapsulation.
• Data hiding is key features of OOPs.
• It can be lead using access specifiers.
•
Private Accessible only its own members
and certain special functions called
as friend functions.
Protected Accessible by members of inherited
classes.
Public Access allowed by other members
in addition to class members and
objects.
Data members and Member functions
• Members of the classes are data or functions.
• Variables are declared inside the class is called
data members. In the above ex: name,regno,
m1,m2 and tot are data members.
• Functions are declared inside the class is called
member functions.
• In above ex:accept(), compute(), display(),
execute() and student() are member functions.
CREATING OBJECTS
• Object is the instance variable of type class.
• Ex: student stud;
• Here stud is the object of user defined type student
class.
• We access the declared data type of student is lead
using object stud.
• ACCESSING CLASS MEMBERS
• Objectname.functionname();
• Ex: stud.execute();
• Dot operators is used to access the class
members.
Ex: using class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
void getdata()
{
a=5;b=6;
sum=a+b;
}
};
void main()
{
add a;//creating object
a.getdata();//access function
cout<<“Sum=”<<s.sum;
}
Output:
Sum =15
Ex: defining method outside of the
class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
void getdata();
};
void add::getdata()//outside
{
a=5;b=6;
sum=a+b;
}
void main()
{
add a;//creating object
a.getdata();//access function
cout<<“Sum=”<<s.sum;
}
Output:
Sum =15
Here add:: tells the compiler
that the function belongs to
the class name add.
Characteristics of member functions
• several different classes can use the same
function name. The membership label will
resolve the problem.
• Member function can access the private data of
a class. Non-member function cannot access it.
• A member function can call another member
function directly.(nesting of member function)
• Member function can receive the arguments of
a valid c++ data type. Object can also be
passed as arguments.
• A member function can return any valid data
type and also objects.
• A member function can be of static type
Memory allocation of Members
• Only one copy of memory allocated for member
functions
• For every objects, separate memory is allocated for
data members
• Ex: add a1,a2;
• 6 bytes for each objects a1 and a2.
• a1 object a2 object methods
a:
b:
Sum:
a:
b:
Sum:
getdata()
{
}
Example program- member function
handle arguments
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c;quantity=q;price=p;
}
void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<q
uanity;
cout<<“nCode=“<<price;
}
};
void main()
{
product p;
p.assign_data(101,200,12.5);
p.display();
}
Static data members
A data member of a class is qualified as static.
Rules:
• Initialized to zero when the first object of
the class is created.
• No other Initialization is allowed.
• Only one copy of member variable is
created. It is shared by all the other objects
of its class type.
• Its scope or visibility is within the class but
its life time is the lifetime of the program
Example program static data members
#include<iostream.h>
class sample
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<“Enter the value….”;
cin>>a>>b;
sum=a+b;
count++;
}
void display()
{
cout<<“n the sum of 2 no“<<sum;
cout<<“nthis is addition “<<count;
}
};
//static member initialized
out side of the class
int sample.count=0;
void main()
{
sample p1,p2,p3;
p1.accept();
p1.display();
p2.accept();
p2.display();
p3.accept();
p3.display();
}
Output:
Enter the values… 2 3
The sum of two numbers 5
This is addition 1
Enter the values… 5 3
The sum of two numbers 8
This is addition 2
Enter the values… 2 5
The sum of two numbers 7
This is addition 3
Rules:
Static member count is
initialized only once
of all the objects of
that class.
It is initialized outside of
the class.
Memory allocation of static
Members
• Only one copy of memory allocated static members of
all the classes
• In the above example p1,p2 and p3 objects
• p1 object p2 object p3 object
• static data members
a:2
b:3
Sum:5
a:5
b:3
Sum8:
a:2
b:5
Sum:7
count:3
Example program- array of objects
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c;quantity=q;price=p;
}
void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<q
uanity;
cout<<“nCode=“<<price;
}
} p[3];
void main()
{
p[0].assign_data(101,200,12.5);
p[0].display();
}
Array of objects
P[0]
Code:
Quantity
Price
P[1]
Code:
Quantity
Price
P[2]
Code:
Quantity
Price
Identify the errors ( Page no 164)
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c, quantity=q,price=p’;
}
void display();
};
Void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<qua
nity;
cout<<“nCode=“<<price;
}
} p[3];
void main()
{
p[0].assign_data(101,200,12.5);
p[0].display();
}
Solution:
void assign_data(int c,int q,float p)
{
code=c; quantity=q;price=p;
}
void product::display()
{
…..
}
Ex: defining method outside of the
class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
protected: void getdata();
};
void add::getdata()
{
a=b=0;
sum=a+b;
}
void main()
{
a=5;
b=6;
add a1;
a1.getdata();
cout<<“Sum=”<<sum;
}
Solution:
Protected member cannot be accessed
outside of the class.
getdata();
Private members cannot be accessed
outside of the class.
a=5
Sum is the public members of class add.
But it is accessed only object of that
class.
a1.sum;
Example program static data members
#include<iostream.h>
class sample
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<“Enter the value….”;
cin>>a>>b;
sum=a+b;
count++;
}
void display()
{
cout<<“n the sum of 2 no“<<sum;
cout<<“nthis is addition “<<count;
}
};
int sample.count=0;
void main()
{
sample p1,p2,p3;
p1.accept();
p1.display();
cout<<p1.count;//error
}

Mais conteúdo relacionado

Semelhante a classes & objects.ppt

C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 

Semelhante a classes & objects.ppt (20)

Oop concepts
Oop conceptsOop concepts
Oop concepts
 
ccc
cccccc
ccc
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
class c++
class c++class c++
class c++
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
class and objects
class and objectsclass and objects
class and objects
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
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
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

classes & objects.ppt

  • 1. CLASSES AND OBJECTS IN C++ by Mrs. B.Arulmozhi, M.Sc., B.Ed., M.Phil., SET., NET., Assistant Professor and Head, Department of Computer Science and Applications D.K.M. College for Women, Vellore
  • 2. SYNPOSIS • Class • Syntax • Data Abstraction • Example • Creating Object • Defining member function inside and outside • Memory allocation of object • Example
  • 3. CLASSES It is a user defined data type. A class is a way to bind the data and its associated functions together. Example:Class student { char name[30]; int rollno,m1,m2,tot; public:void calculate(); };
  • 4. Specifying class: It has 2 parts. 1. class declaration 2. class function definitions. Syntax: Class class-name { private: variable-declaration; function-declaration; public: variable-declaration; function-declaration; protected: variable-declaration; function-declaration; }
  • 5. Example class student { private: char name[20]; int roll,m1,m2,tot; protected: void accept(); void compute(); void display(); public: student(); void execute(); };
  • 6. Rules • class is a keyword • Body of the class enclosed within braces and terminated by ; • Body contains declarations of variables and functions • It contain 3 access specifiers.(visibility labels) • private, public and protected • private members accessed only with in the class. • public members accessed outside of the class also. • protected members accessed the members of the inherted classes only
  • 7. Data abstraction • The binding of data and functions together into a single entity is referred to as encapsulation. • Data hiding is key features of OOPs. • It can be lead using access specifiers. • Private Accessible only its own members and certain special functions called as friend functions. Protected Accessible by members of inherited classes. Public Access allowed by other members in addition to class members and objects.
  • 8. Data members and Member functions • Members of the classes are data or functions. • Variables are declared inside the class is called data members. In the above ex: name,regno, m1,m2 and tot are data members. • Functions are declared inside the class is called member functions. • In above ex:accept(), compute(), display(), execute() and student() are member functions.
  • 9. CREATING OBJECTS • Object is the instance variable of type class. • Ex: student stud; • Here stud is the object of user defined type student class. • We access the declared data type of student is lead using object stud. • ACCESSING CLASS MEMBERS • Objectname.functionname(); • Ex: stud.execute(); • Dot operators is used to access the class members.
  • 10. Ex: using class #include <iostream.h> Class add { private: int a,b; public:int sum; void getdata() { a=5;b=6; sum=a+b; } }; void main() { add a;//creating object a.getdata();//access function cout<<“Sum=”<<s.sum; } Output: Sum =15
  • 11. Ex: defining method outside of the class #include <iostream.h> Class add { private: int a,b; public:int sum; void getdata(); }; void add::getdata()//outside { a=5;b=6; sum=a+b; } void main() { add a;//creating object a.getdata();//access function cout<<“Sum=”<<s.sum; } Output: Sum =15 Here add:: tells the compiler that the function belongs to the class name add.
  • 12. Characteristics of member functions • several different classes can use the same function name. The membership label will resolve the problem. • Member function can access the private data of a class. Non-member function cannot access it. • A member function can call another member function directly.(nesting of member function) • Member function can receive the arguments of a valid c++ data type. Object can also be passed as arguments. • A member function can return any valid data type and also objects. • A member function can be of static type
  • 13. Memory allocation of Members • Only one copy of memory allocated for member functions • For every objects, separate memory is allocated for data members • Ex: add a1,a2; • 6 bytes for each objects a1 and a2. • a1 object a2 object methods a: b: Sum: a: b: Sum: getdata() { }
  • 14. Example program- member function handle arguments #include<iostream.h> class product { int code,quantity; float price; public: void assign_data(int c,int q,float p) { code=c;quantity=q;price=p; } void display() { cout<<“nCode=“<<code; cout<<“nQuantity=“<<q uanity; cout<<“nCode=“<<price; } }; void main() { product p; p.assign_data(101,200,12.5); p.display(); }
  • 15. Static data members A data member of a class is qualified as static. Rules: • Initialized to zero when the first object of the class is created. • No other Initialization is allowed. • Only one copy of member variable is created. It is shared by all the other objects of its class type. • Its scope or visibility is within the class but its life time is the lifetime of the program
  • 16. Example program static data members #include<iostream.h> class sample { int a,b,sum; static int count; public: void accept() { cout<<“Enter the value….”; cin>>a>>b; sum=a+b; count++; } void display() { cout<<“n the sum of 2 no“<<sum; cout<<“nthis is addition “<<count; } }; //static member initialized out side of the class int sample.count=0; void main() { sample p1,p2,p3; p1.accept(); p1.display(); p2.accept(); p2.display(); p3.accept(); p3.display(); }
  • 17. Output: Enter the values… 2 3 The sum of two numbers 5 This is addition 1 Enter the values… 5 3 The sum of two numbers 8 This is addition 2 Enter the values… 2 5 The sum of two numbers 7 This is addition 3 Rules: Static member count is initialized only once of all the objects of that class. It is initialized outside of the class.
  • 18. Memory allocation of static Members • Only one copy of memory allocated static members of all the classes • In the above example p1,p2 and p3 objects • p1 object p2 object p3 object • static data members a:2 b:3 Sum:5 a:5 b:3 Sum8: a:2 b:5 Sum:7 count:3
  • 19. Example program- array of objects #include<iostream.h> class product { int code,quantity; float price; public: void assign_data(int c,int q,float p) { code=c;quantity=q;price=p; } void display() { cout<<“nCode=“<<code; cout<<“nQuantity=“<<q uanity; cout<<“nCode=“<<price; } } p[3]; void main() { p[0].assign_data(101,200,12.5); p[0].display(); }
  • 21. Identify the errors ( Page no 164) #include<iostream.h> class product { int code,quantity; float price; public: void assign_data(int c,int q,float p) { code=c, quantity=q,price=p’; } void display(); }; Void display() { cout<<“nCode=“<<code; cout<<“nQuantity=“<<qua nity; cout<<“nCode=“<<price; } } p[3]; void main() { p[0].assign_data(101,200,12.5); p[0].display(); }
  • 22. Solution: void assign_data(int c,int q,float p) { code=c; quantity=q;price=p; } void product::display() { ….. }
  • 23. Ex: defining method outside of the class #include <iostream.h> Class add { private: int a,b; public:int sum; protected: void getdata(); }; void add::getdata() { a=b=0; sum=a+b; } void main() { a=5; b=6; add a1; a1.getdata(); cout<<“Sum=”<<sum; }
  • 24. Solution: Protected member cannot be accessed outside of the class. getdata(); Private members cannot be accessed outside of the class. a=5 Sum is the public members of class add. But it is accessed only object of that class. a1.sum;
  • 25. Example program static data members #include<iostream.h> class sample { int a,b,sum; static int count; public: void accept() { cout<<“Enter the value….”; cin>>a>>b; sum=a+b; count++; } void display() { cout<<“n the sum of 2 no“<<sum; cout<<“nthis is addition “<<count; } }; int sample.count=0; void main() { sample p1,p2,p3; p1.accept(); p1.display(); cout<<p1.count;//error }