SlideShare a Scribd company logo
1 of 19
Presented by:
Zahid Tanveer FA13-BSE-029
 Programmer thinks about and defines the attributes
and behavior of objects.
 Often the objects are modeled after real-world entities.
 Very different approach than function-based
programming (like C).
 Object-oriented programming (OOP)
 Encapsulates data (attributes) and functions (behavior)
into packages called classes.
 So, Classes are user-defined (programmer-defined)
types.
 Data (data members)
 Functions (member functions or methods)
 In other words, they are structures + functions
 A class definition begins with the keyword class.
 The body of the class is contained within a set of
braces, { } ; (notice the semi-colon).
class class_name
{
….
….
….
};
Class body (data member
+ methods)
Any valid
identifier
 Within the body, the keywords private: and public:
specify the access level of the members of the class.
 the default is private.
 Usually, the data members of a class are declared in
the private: section of the class and the member
functions are in public: section.
class class_name
{
private:
…
…
…
public:
…
…
…
};
Public members or methods
private members or
methods
 Member access specifiers
 public:
 can be accessed outside the class directly.
 The public stuff is the interface.
 private:
 Accessible only to member functions of class
 Private members and methods are for internal use only.
 This class example shows how we can encapsulate
(gather) a circle information into one package (unit or
class)
class Circle
{
private:
double radius;
public:
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
No need for others classes to
access and retrieve its value
directly. The class methods are
responsible for that only.
They are accessible from outside
the class, and they can access the
member (radius)
 Declaring a variable of a class type creates an
object. You can have many variables of the same
type (class).
 Instantiation
 Once an object of a certain class is instantiated, a
new memory location is created for it to store its
data members and code
 You can instantiate many objects from a class type.
 Ex) Circle c;
 Constructor:
 Public function member
 called when a new object is created (instantiated).
 Initialize data members.
 Same name as class
 No return type
 Several constructors
 Function overloading
class Circle
{
private:
double radius;
public:
Circle();
Circle(int r);
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
Constructor with no
argument
Constructor with one
argument
 Class implementation: writing the code of class
methods.
 There are two ways:
1. Member functions defined outside class
 Using Binary scope resolution operator (::)
 “Ties” member name to class name
 Uniquely identify functions of particular class
 Different classes can have member functions with same
name
 Format for defining member functions
ReturnType ClassName::MemberFunctionName( ){
…
}
2. Member functions defined inside class
 Do not need scope resolution operator, class
name;
class Circle
{
private:
double radius;
public:
Circle() { radius = 0.0;}
Circle(int r);
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
Defined
inside
class
class Circle
{
private:
double radius;
public:
Circle() { radius = 0.0;}
Circle(int r);
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
Circle::Circle(int r)
{
radius = r;
}
double Circle::getArea()
{
return radius * radius * (22.0/7);
}
double Circle:: getCircumference()
{
return 2 * radius * (22.0/7);
}
Defined outside class
 Operators to access class members
 Identical to those for structs
 Dot member selection operator (.)
 Object
 Reference to object
class Circle
{
private:
double radius;
public:
Circle() { radius = 0.0;}
Circle(int r);
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
Circle::Circle(int r)
{
radius = r;
}
double Circle::getArea()
{
return radius * radius * (22.0/7);
}
double Circle:: getCircumference()
{
return 2 * radius * (22.0/7);
}
void main()
{
Circle c1,c2(7);
cout<<“The area of c1:”
<<c1.getArea()<<“n”;
//c1.raduis = 5;//syntax error
c1.setRadius(5);
cout<<“The circumference of c1:”
<< c1.getCircumference()<<“n”;
cout<<“The Diameter of c2:”
<<c2.getDiameter()<<“n”;
}
The first
constructor is
called
The second
constructor is
called
Since radius is a
private class data
member
 Destructors
 Special member function
 Same name as class
 Preceded with tilde (~)
 No arguments
 No return value
 Cannot be overloaded
 Before system reclaims object’s memory
 Reuse memory for new objects
 Mainly used to de-allocate dynamic memory locations
 This class shows
#include<iostream>
Using namespace std;
class Test
{
private:
int n;
public:
Test()
{
cout<<“Object is Created………”<<endl;
}
~Test()
{
cout<<“object is destroyed”<<endl;
}
};
Void main()
{
Test a,b;
getchar();
Getchar();
}
Destructor
C++ classes

More Related Content

What's hot

oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectOUM SAOKOSAL
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in javaHarish Gyanani
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceOUM SAOKOSAL
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1sai kumar
 

What's hot (19)

Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Unit i
Unit iUnit i
Unit i
 
oops-1
oops-1oops-1
oops-1
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Inheritance
InheritanceInheritance
Inheritance
 
Java02
Java02Java02
Java02
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in java
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Lecture 2 classes i
Lecture 2 classes iLecture 2 classes i
Lecture 2 classes i
 
9 cm604.14
9 cm604.149 cm604.14
9 cm604.14
 
Java oop
Java oopJava oop
Java oop
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
PHP OOP
PHP OOPPHP OOP
PHP OOP
 
Classes
ClassesClasses
Classes
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 

Viewers also liked (6)

Saraf & Thakur
Saraf  & ThakurSaraf  & Thakur
Saraf & Thakur
 
C++programming howto
C++programming howtoC++programming howto
C++programming howto
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
 
C++primer
C++primerC++primer
C++primer
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 

Similar to C++ classes

C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsFALLEE31188
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialskksupaul
 
C++ Classes Tutorials.ppt
C++ Classes Tutorials.pptC++ Classes Tutorials.ppt
C++ Classes Tutorials.pptaasuran
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their AccessingMuhammad Hammad Waseem
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 

Similar to C++ classes (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ classes
C++ classesC++ classes
C++ classes
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ Classes Tutorials.ppt
C++ Classes Tutorials.pptC++ Classes Tutorials.ppt
C++ Classes Tutorials.ppt
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 
Oo ps
Oo psOo ps
Oo ps
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Class and object
Class and objectClass and object
Class and object
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 

Recently uploaded

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 

C++ classes

  • 2.  Programmer thinks about and defines the attributes and behavior of objects.  Often the objects are modeled after real-world entities.  Very different approach than function-based programming (like C).
  • 3.  Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior) into packages called classes.  So, Classes are user-defined (programmer-defined) types.  Data (data members)  Functions (member functions or methods)  In other words, they are structures + functions
  • 4.  A class definition begins with the keyword class.  The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Class body (data member + methods) Any valid identifier
  • 5.  Within the body, the keywords private: and public: specify the access level of the members of the class.  the default is private.  Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.
  • 7.  Member access specifiers  public:  can be accessed outside the class directly.  The public stuff is the interface.  private:  Accessible only to member functions of class  Private members and methods are for internal use only.
  • 8.  This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)
  • 9.  Declaring a variable of a class type creates an object. You can have many variables of the same type (class).  Instantiation  Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code  You can instantiate many objects from a class type.  Ex) Circle c;
  • 10.  Constructor:  Public function member  called when a new object is created (instantiated).  Initialize data members.  Same name as class  No return type  Several constructors  Function overloading
  • 11. class Circle { private: double radius; public: Circle(); Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; Constructor with no argument Constructor with one argument
  • 12.  Class implementation: writing the code of class methods.  There are two ways: 1. Member functions defined outside class  Using Binary scope resolution operator (::)  “Ties” member name to class name  Uniquely identify functions of particular class  Different classes can have member functions with same name  Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ … }
  • 13. 2. Member functions defined inside class  Do not need scope resolution operator, class name; class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Defined inside class
  • 14. class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } Defined outside class
  • 15.  Operators to access class members  Identical to those for structs  Dot member selection operator (.)  Object  Reference to object
  • 16. class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } void main() { Circle c1,c2(7); cout<<“The area of c1:” <<c1.getArea()<<“n”; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<<“The circumference of c1:” << c1.getCircumference()<<“n”; cout<<“The Diameter of c2:” <<c2.getDiameter()<<“n”; } The first constructor is called The second constructor is called Since radius is a private class data member
  • 17.  Destructors  Special member function  Same name as class  Preceded with tilde (~)  No arguments  No return value  Cannot be overloaded  Before system reclaims object’s memory  Reuse memory for new objects  Mainly used to de-allocate dynamic memory locations
  • 18.  This class shows #include<iostream> Using namespace std; class Test { private: int n; public: Test() { cout<<“Object is Created………”<<endl; } ~Test() { cout<<“object is destroyed”<<endl; } }; Void main() { Test a,b; getchar(); Getchar(); } Destructor