SlideShare a Scribd company logo
C++ Classes  & Object Oriented Programming
Object Oriented Programming 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 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. The public stuff is  the interface . 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)
Creating an object of a Class 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; Circle *c;
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: 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 ( ){ … }
Implementing class methods 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
Accessing Class Members Operators to access class members Identical to those for  struct s Dot member selection operator ( . ) Object Reference to object 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(); }
Destructors 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
Another class Example This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor
Time::Time() { hour = new int; minute = new int; second = new int; * hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; * hour = h; * minute = m; * second = s; } void Time::setTime(int h,int m,int s) { * hour = h; * minute = m; * second = s; } Dynamic locations  should be allocated to pointers first
void Time :: printTime () { cout<< &quot; The time is  : (&quot; << * hour<< &quot;:&quot; << * minute<< &quot;:&quot; << * second<< &quot;)&quot; << endl; } Time :: ~Time () { delete hour; delete minute;delete second; } void main () { Time  * t; t =  new Time ( 3,55,54 ) ; t - >printTime () ; t - >setHour ( 7 ) ; t - >setMinute ( 17 ) ; t - >setSecond ( 43 ) ; t - >printTime () ; delete t; } Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue Destructor: used here to de-allocate memory locations When executed, the destructor is called
Reasons for OOP Simplify programming Interfaces Information hiding: Implementation details hidden within classes themselves Software reuse Class objects included as members of other classes

More Related Content

PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
20.3 Java encapsulation
PPTX
Introduccion a Java
PPT
C Basics
PPTX
C# classes objects
PPT
C PROGRAMMING
PPT
Structure of C++ - R.D.Sivakumar
PPTX
classes and objects in C++
operator overloading & type conversion in cpp over view || c++
20.3 Java encapsulation
Introduccion a Java
C Basics
C# classes objects
C PROGRAMMING
Structure of C++ - R.D.Sivakumar
classes and objects in C++

What's hot (20)

PPT
C++ Pointers And References
PDF
2. Manejo de la sintaxis del lenguaje
PDF
Operator Overloading in C++
PPS
Interface
PDF
Python programming : Classes objects
PPTX
C programming - Pointers
PPT
OOP V3.1
PPTX
Sax parser
PDF
Operator overloading C++
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PPTX
C programming language tutorial
PDF
Access specifiers (Public Private Protected) C++
PPTX
Oop c++class(final).ppt
PPT
Java Basics
DOCX
PPTX
Constructors in C++
PDF
Methods in Java
PPTX
Classes, objects in JAVA
PPTX
Virtual base class
PPT
Strings
C++ Pointers And References
2. Manejo de la sintaxis del lenguaje
Operator Overloading in C++
Interface
Python programming : Classes objects
C programming - Pointers
OOP V3.1
Sax parser
Operator overloading C++
What is Python Lambda Function? Python Tutorial | Edureka
C programming language tutorial
Access specifiers (Public Private Protected) C++
Oop c++class(final).ppt
Java Basics
Constructors in C++
Methods in Java
Classes, objects in JAVA
Virtual base class
Strings
Ad

Viewers also liked (20)

PPT
C++ classes
PPT
Oops ppt
PPT
Basic concepts of object oriented programming
PPT
Object oriented programming (oop) cs304 power point slides lecture 01
PPTX
Introduction to Object Oriented Programming
PPT
C++ classes tutorials
PPT
Object Oriented Programming Concepts
PPTX
class and objects
PPT
Object-oriented concepts
PPTX
Classes and objects till 16 aug
PPTX
C++ classes
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PDF
Intro to C++ - language
PPTX
Inheritance
PPTX
functions of C++
PDF
C++ lecture notes
PDF
3. c++ by example
PPTX
C++ good tutorial
PDF
Introduction to Docker
C++ classes
Oops ppt
Basic concepts of object oriented programming
Object oriented programming (oop) cs304 power point slides lecture 01
Introduction to Object Oriented Programming
C++ classes tutorials
Object Oriented Programming Concepts
class and objects
Object-oriented concepts
Classes and objects till 16 aug
C++ classes
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Intro to C++ - language
Inheritance
functions of C++
C++ lecture notes
3. c++ by example
C++ good tutorial
Introduction to Docker
Ad

Similar to C++ classes tutorials (20)

PPT
C++ Classes Tutorials.ppt
PPT
UNIT I (1).ppt
PPT
UNIT I (1).ppt
PDF
CLASSES and OBJECTS in C++ detailed explanation
PPT
C++ classes tutorials
PPT
C++ classes tutorials
PPTX
C++ classes
PPTX
Classes and objects
PPTX
Classes and objects
PPTX
Oop objects_classes
PPTX
05 Object Oriented Concept Presentation.pptx
PDF
Chapter22 static-class-member-example
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPT
Oops concept in c#
PPT
Bca 2nd sem u-2 classes & objects
PPT
Mca 2nd sem u-2 classes & objects
PPT
Chapter 4 - Defining Your Own Classes - Part I
PPT
Java căn bản - Chapter4
PPTX
OBJECT ORIENTED PROGRAMING IN C++
C++ Classes Tutorials.ppt
UNIT I (1).ppt
UNIT I (1).ppt
CLASSES and OBJECTS in C++ detailed explanation
C++ classes tutorials
C++ classes tutorials
C++ classes
Classes and objects
Classes and objects
Oop objects_classes
05 Object Oriented Concept Presentation.pptx
Chapter22 static-class-member-example
[OOP - Lec 09,10,11] Class Members & their Accessing
Oops concept in c#
Bca 2nd sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Chapter 4 - Defining Your Own Classes - Part I
Java căn bản - Chapter4
OBJECT ORIENTED PROGRAMING IN C++

More from FALLEE31188 (20)

PPT
Lecture4
PPT
Lecture2
PPT
PPTX
Inheritance
PDF
Inheritance
PPT
Functions
DOC
Field name
PPT
Encapsulation
PPT
Cpp tutorial
PPT
Cis068 08
PPT
Chapter14
PPT
Chapt03
PPT
C++lecture9
PPT
C++ polymorphism
PPT
C1320prespost
PPT
Brookshear 06
PPT
Book ppt
PPTX
Assignment 2
PPTX
Assignment
Lecture4
Lecture2
Inheritance
Inheritance
Functions
Field name
Encapsulation
Cpp tutorial
Cis068 08
Chapter14
Chapt03
C++lecture9
C++ polymorphism
C1320prespost
Brookshear 06
Book ppt
Assignment 2
Assignment

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Tartificialntelligence_presentation.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Spectroscopy.pptx food analysis technology
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
Tartificialntelligence_presentation.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
Spectroscopy.pptx food analysis technology
SOPHOS-XG Firewall Administrator PPT.pptx
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars

C++ classes tutorials

  • 1. C++ Classes & Object Oriented Programming
  • 2. Object Oriented Programming 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 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. 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
  • 5. 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.
  • 6. Classes in C++ class class_name { private: … … … public: … … … }; Public members or methods private members or methods
  • 7. Classes in C++ 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. 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)
  • 9. Creating an object of a Class 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; Circle *c;
  • 10. 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
  • 11. 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
  • 12. Implementing class methods Class implementation: writing the code of class methods. There are two ways: 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. Implementing class methods 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. Accessing Class Members Operators to access class members Identical to those for struct s Dot member selection operator ( . ) Object Reference to object 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(); }
  • 18. Destructors 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
  • 19. Another class Example This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor
  • 20. Time::Time() { hour = new int; minute = new int; second = new int; * hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; * hour = h; * minute = m; * second = s; } void Time::setTime(int h,int m,int s) { * hour = h; * minute = m; * second = s; } Dynamic locations should be allocated to pointers first
  • 21. void Time :: printTime () { cout<< &quot; The time is : (&quot; << * hour<< &quot;:&quot; << * minute<< &quot;:&quot; << * second<< &quot;)&quot; << endl; } Time :: ~Time () { delete hour; delete minute;delete second; } void main () { Time * t; t = new Time ( 3,55,54 ) ; t - >printTime () ; t - >setHour ( 7 ) ; t - >setMinute ( 17 ) ; t - >setSecond ( 43 ) ; t - >printTime () ; delete t; } Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue Destructor: used here to de-allocate memory locations When executed, the destructor is called
  • 22. Reasons for OOP Simplify programming Interfaces Information hiding: Implementation details hidden within classes themselves Software reuse Class objects included as members of other classes