SlideShare a Scribd company logo
9
Most read
11
Most read
14
Most read
Class Members
Data Members
Member Functions (Methods)
Accessing Class Members
Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
Object Oriented Programming
• 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
Classes in C++
• 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
Classes in C++
• 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.
Classes in C++
class class_name
{
private:
…
…
…
public:
…
…
…
};
Public members or methods
private members or
methods
Classes in C++
• Member access specifiers
• public:
• can be accessed outside the class directly.
• private:
• Accessible only to member functions of class
• Private members and methods are for internal use only.
Class Example
• 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)
Special Member Functions
• 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
Special Member Functions
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
Implementing class methods
• Class implementation: writing the code of class methods.
• There are two ways:
1. Member functions defined inside class
MemberFunctionName( )
{
…
}
2. Member functions defined outside class
ReturnType ClassName::MemberFunctionName( )
{
…
}
Implementing class methods
1. 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
Defining Member Functions outside Class
• The member function of a class can also be defined outside the class.
• The declaration of member functions is specified within the class and
function definition is specified outside the class.
• The scope resolution operator :: is used in function declaration if the
function is defined outside the class.
Syntax
• The syntax of defining member function outside the class is as follows:
Return_type Class_name:: function_name(params)
{
Function body
}
• Return_type
• It indicates the type of value to be returned by the function.
• Class_name
• It indicates the name of class to which the function belongs.
• ::
• It is the scope resolution operator to define member function outside the class.
• function_name
• It is the name of the member function to be defined.
• Function body
• It is the body of the function.
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
Accessing Class Members
• Operators to access class members
• Identical to those for structs
• Dot member selection operator (.)
• Arrow member selection operator (->)
• Pointers
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
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 c(7);
Circle *cp1 = &c;
Circle *cp2 = new Circle(7);
cout<<“The are of cp2:”
<<cp2->getArea();
}

More Related Content

What's hot (20)

PDF
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
Seok-joon Yun
 
PPTX
This pointer
Kamal Acharya
 
PPT
Static and Dynamic polymorphism in C++
Anil Bapat
 
PDF
POO Java Chapitre 4 Heritage et Polymorphisme
Mouna Torjmen
 
PDF
Les pointeurs en C/C++
Ammar Ladjailia
 
PPT
programmation orienté objet c++
coursuniv
 
PDF
Chapitre 4 heritage et polymorphisme
Amir Souissi
 
PDF
Chap1: Cours en C++
Aziz Darouichi
 
PDF
Chapitre5: Classes et objets
Aziz Darouichi
 
PDF
Chapitre6: Surcharge des opérateurs
Aziz Darouichi
 
PPTX
inheritance c++
Muraleedhar Sundararajan
 
PPT
Functions in C++
Mohammed Sikander
 
PDF
POO Java Chapitre 1 Classe & Objet
Mouna Torjmen
 
PPTX
Virtual base class
Tech_MX
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PDF
POO Java Chapitre 2 Encapsulation
Mouna Torjmen
 
PDF
2nd puc computer science chapter 8 function overloading
Aahwini Esware gowda
 
PPT
Method overriding
Azaz Maverick
 
PPTX
Templates in C++
Tech_MX
 
PPT
Java static keyword
Lovely Professional University
 
[C++ korea] Effective Modern C++ 신촌 Study Item20,21,23
Seok-joon Yun
 
This pointer
Kamal Acharya
 
Static and Dynamic polymorphism in C++
Anil Bapat
 
POO Java Chapitre 4 Heritage et Polymorphisme
Mouna Torjmen
 
Les pointeurs en C/C++
Ammar Ladjailia
 
programmation orienté objet c++
coursuniv
 
Chapitre 4 heritage et polymorphisme
Amir Souissi
 
Chap1: Cours en C++
Aziz Darouichi
 
Chapitre5: Classes et objets
Aziz Darouichi
 
Chapitre6: Surcharge des opérateurs
Aziz Darouichi
 
inheritance c++
Muraleedhar Sundararajan
 
Functions in C++
Mohammed Sikander
 
POO Java Chapitre 1 Classe & Objet
Mouna Torjmen
 
Virtual base class
Tech_MX
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
POO Java Chapitre 2 Encapsulation
Mouna Torjmen
 
2nd puc computer science chapter 8 function overloading
Aahwini Esware gowda
 
Method overriding
Azaz Maverick
 
Templates in C++
Tech_MX
 
Java static keyword
Lovely Professional University
 

Viewers also liked (18)

PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
PDF
C++ largest no between three nos
krismishra
 
PPTX
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 03] Programming Paradigms
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 02] Why do we need OOP
Muhammad Hammad Waseem
 
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
Data structure and its types
Navtar Sidhu Brar
 
PPT
constructor and destructor-object oriented programming
Ashita Agrawal
 
PDF
2015 Upload Campaigns Calendar - SlideShare
SlideShare
 
PPTX
What to Upload to SlideShare
SlideShare
 
PDF
Getting Started With SlideShare
SlideShare
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
C++ largest no between three nos
krismishra
 
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
[OOP - Lec 03] Programming Paradigms
Muhammad Hammad Waseem
 
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
[OOP - Lec 02] Why do we need OOP
Muhammad Hammad Waseem
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Data structure and its types
Navtar Sidhu Brar
 
constructor and destructor-object oriented programming
Ashita Agrawal
 
2015 Upload Campaigns Calendar - SlideShare
SlideShare
 
What to Upload to SlideShare
SlideShare
 
Getting Started With SlideShare
SlideShare
 
Ad

Similar to [OOP - Lec 09,10,11] Class Members & their Accessing (20)

PPTX
C++ classes
Zahid Tanveer
 
PPT
C++ classes tutorials
Mayank Jain
 
PDF
CLASSES and OBJECTS in C++ detailed explanation
gayathrid52
 
PPT
C++ Classes Tutorials.ppt
aasuran
 
PPTX
C++ classes
Aayush Patel
 
PPT
Class and object in C++
rprajat007
 
PPT
UNIT I (1).ppt
VGaneshKarthikeyan
 
PPT
UNIT I (1).ppt
VGaneshKarthikeyan
 
PPTX
Classes and objects
rajveer_Pannu
 
PPT
C++ classes tutorials
kailash454
 
PPT
C++ classes tutorials
kksupaul
 
PPTX
Classes and objects
Anil Kumar
 
PDF
Chapter18 class-and-objects
Deepak Singh
 
PPT
C++ classes tutorials
FALLEE31188
 
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
 
PPTX
Oop objects_classes
sidra tauseef
 
PPT
Classes and objects
Lovely Professional University
 
PPT
4 Classes & Objects
Praveen M Jigajinni
 
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
C++ classes
Zahid Tanveer
 
C++ classes tutorials
Mayank Jain
 
CLASSES and OBJECTS in C++ detailed explanation
gayathrid52
 
C++ Classes Tutorials.ppt
aasuran
 
C++ classes
Aayush Patel
 
Class and object in C++
rprajat007
 
UNIT I (1).ppt
VGaneshKarthikeyan
 
UNIT I (1).ppt
VGaneshKarthikeyan
 
Classes and objects
rajveer_Pannu
 
C++ classes tutorials
kailash454
 
C++ classes tutorials
kksupaul
 
Classes and objects
Anil Kumar
 
Chapter18 class-and-objects
Deepak Singh
 
C++ classes tutorials
FALLEE31188
 
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
 
Oop objects_classes
sidra tauseef
 
Classes and objects
Lovely Professional University
 
4 Classes & Objects
Praveen M Jigajinni
 
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Ad

More from Muhammad Hammad Waseem (18)

PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
PPTX
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 

Recently uploaded (20)

PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 

[OOP - Lec 09,10,11] Class Members & their Accessing

  • 1. Class Members Data Members Member Functions (Methods) Accessing Class Members Muhammad Hammad Waseem [email protected]
  • 2. Object Oriented Programming • 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
  • 3. Classes in C++ • 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
  • 4. Classes in C++ • 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.
  • 5. Classes in C++ class class_name { private: … … … public: … … … }; Public members or methods private members or methods
  • 6. Classes in C++ • Member access specifiers • public: • can be accessed outside the class directly. • private: • Accessible only to member functions of class • Private members and methods are for internal use only.
  • 7. Class Example • 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)
  • 8. Special Member Functions • 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
  • 9. Special Member Functions 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
  • 10. Implementing class methods • Class implementation: writing the code of class methods. • There are two ways: 1. Member functions defined inside class MemberFunctionName( ) { … } 2. Member functions defined outside class ReturnType ClassName::MemberFunctionName( ) { … }
  • 11. Implementing class methods 1. 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
  • 12. Defining Member Functions outside Class • The member function of a class can also be defined outside the class. • The declaration of member functions is specified within the class and function definition is specified outside the class. • The scope resolution operator :: is used in function declaration if the function is defined outside the class.
  • 13. Syntax • The syntax of defining member function outside the class is as follows: Return_type Class_name:: function_name(params) { Function body } • Return_type • It indicates the type of value to be returned by the function. • Class_name • It indicates the name of class to which the function belongs. • :: • It is the scope resolution operator to define member function outside the class. • function_name • It is the name of the member function to be defined. • Function body • It is the body of the function.
  • 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. Accessing Class Members • Operators to access class members • Identical to those for structs • Dot member selection operator (.) • Arrow member selection operator (->) • Pointers
  • 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. 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 c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7); cout<<“The are of cp2:” <<cp2->getArea(); }