SlideShare a Scribd company logo
Name – Rajveer Kaur
Section – N2
Roll No. - 115312
 Classes   in C++
 Objects
 Creating an object of class
 Special member functions
 Implementing class methods
 Accessing class members
 Destructors
 Instance variable methods
 Class abstraction
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       Any valid
                    {                      identifier
                              ….
                    ….
                                       Class body (data member +
                    ….
                                       methods)
                    };
Objects

  • Objects have three
  responsibilities:
 What they know about themselves – (e.g., Attributes)
 What they do – (e.g., Operations)
What they know about other objects – (e.g., Relationships)




                                                    4
Defining Class
  A CLASS is a template (specification, blueprint)
  for a collection of objects that share a common
  set of attributes and operations.



                                 HealthClubMember
                                      attributes
Class                                 operations




Objects



                                                     5
 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.
 Thisclass example shows how we can
 encapsulate (gather) a circle information
 into one package (unit or class)
                                            No need for others classes to
    class Circle                            access and retrieve its value
                                            directly. The
    {
                                            class methods are responsible for
       private:                             that only.
              double radius;
       public:
              void setRadius(double r);    They are accessible from outside
              double getDiameter();        the class, and they can access the
              double getArea();            member (radius)
              double getCircumference();
    };
 Declaringa 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;
 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
                                       Constructor with no
{
                                       argument
   private:
          double radius;
   public:                             Constructor with one
          Circle();                    argument
          Circle(int r);
           void setRadius(double r);
          double getDiameter();
          double getArea();
          double getCircumference();
};
       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
          {                                                    Defined
             private:                                          inside
                    double radius;                             class
             public:
                    Circle() { radius = 0.0;}
                    Circle(int r);
                    void setRadius(double r){radius = r;}
                    double getDiameter(){ return radius *2;}
                    double getArea();
                    double getCircumference();
          };
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);
}
 Operators         to access class members
    Identical to those for structs
    Dot member selection operator (.)
        Object
        Reference to object
    Arrow member selection operator (->)
        Pointers
 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 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;}
  Destructor           int getSecond(){return *second;}
                       void setHour(int h){*hour = h;}
                       void setMinute(int m){*minute = m;}
                       void setSecond(int s){*second = s;}
                       ~Time();
               };
Instance variables belong to a specific
instance.

Instance methods are invoked by an instance
of the class.
Class variables are shared by all the instances
of the class.

Class methods are not tied to a specific object.
Class constants are final variables shared by all
the instances of the class.
 The   scope of instance and class variables is
    the entire class. They can be declared
    anywhere inside a class.
    The scope of a local variable starts from its
    declaration and continues to the end of the
    block that contains the variable. A local
    variable must be declared before it can be
    used.
 Use   this to refer to the current object.
 Usethis to invoke other constructors of the
 object.
Class abstraction means to separate class
implementation from the use of the class. The
creator of the class provides a description of the class
and let the user know how the class can be used. The
user of the class does not need to know how the class
is implemented. The detail of implementation is
encapsulated and hidden from the user.
THANKS

More Related Content

PPT
Class and object in C++
PPTX
C++ And Object in lecture3
PPTX
C++ classes
PPT
4 Classes & Objects
PPTX
Classes and objects
PDF
Class and object in C++ By Pawan Thakur
PPTX
Class or Object
PPTX
Classes and objects till 16 aug
Class and object in C++
C++ And Object in lecture3
C++ classes
4 Classes & Objects
Classes and objects
Class and object in C++ By Pawan Thakur
Class or Object
Classes and objects till 16 aug

What's hot (20)

PDF
How to write you first class in c++ object oriented programming
PPTX
Classes in c++ (OOP Presentation)
PPT
Lect 1-class and object
PPTX
Classes and objects in c++
PPT
Object and class
PDF
ITFT-Classes and object in java
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPTX
classes and objects in C++
PPTX
Functions, classes & objects in c++
PDF
Lect 1-java object-classes
PDF
Classes and objects
PPTX
class c++
PPTX
Java class,object,method introduction
PPT
Java: Objects and Object References
PPT
Class and object in c++
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Data members and member functions
PPTX
Pi j3.2 polymorphism
PDF
Class and Objects in Java
PPT
C++ classes
How to write you first class in c++ object oriented programming
Classes in c++ (OOP Presentation)
Lect 1-class and object
Classes and objects in c++
Object and class
ITFT-Classes and object in java
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
classes and objects in C++
Functions, classes & objects in c++
Lect 1-java object-classes
Classes and objects
class c++
Java class,object,method introduction
Java: Objects and Object References
Class and object in c++
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Data members and member functions
Pi j3.2 polymorphism
Class and Objects in Java
C++ classes
Ad

Viewers also liked (20)

PPTX
Chapter 2.datatypes and operators
PPT
New operator and methods.15
PPT
Unit 5 Java
PPTX
Operators
PPT
Unit 4 Java
PPT
Console Io Operations
PDF
Template at c++
PPT
Unit 1 Java
PPTX
Managing console
PPTX
constructor & destructor in cpp
PPTX
Mca ii dfs u-1 introduction to data structure
PPTX
Templates in c++
PPT
Unit 2 Java
DOCX
C++ Template
PPT
Unit 3 Java
PPT
Operators in C++
PPT
08 c++ Operator Overloading.ppt
PPTX
Pointers in c++
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
Templates in C++
Chapter 2.datatypes and operators
New operator and methods.15
Unit 5 Java
Operators
Unit 4 Java
Console Io Operations
Template at c++
Unit 1 Java
Managing console
constructor & destructor in cpp
Mca ii dfs u-1 introduction to data structure
Templates in c++
Unit 2 Java
C++ Template
Unit 3 Java
Operators in C++
08 c++ Operator Overloading.ppt
Pointers in c++
operator overloading & type conversion in cpp over view || c++
Templates in C++
Ad

Similar to Classes and objects (20)

PPT
C++ classes tutorials
PPT
C++ classes tutorials
PPTX
C++ classes
PPT
C++ Classes Tutorials.ppt
PPT
C++ classes tutorials
PDF
CLASSES and OBJECTS in C++ detailed explanation
PPT
C++ classes tutorials
PPT
UNIT I (1).ppt
PPT
UNIT I (1).ppt
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
oopusingc.pptx
PPT
oops-1
PPT
Unit i
PPTX
Oop objects_classes
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PDF
Lab 4 (1).pdf
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PDF
Chapter18 class-and-objects
PPT
Object Oriented Programming Examples with explanation
C++ classes tutorials
C++ classes tutorials
C++ classes
C++ Classes Tutorials.ppt
C++ classes tutorials
CLASSES and OBJECTS in C++ detailed explanation
C++ classes tutorials
UNIT I (1).ppt
UNIT I (1).ppt
[OOP - Lec 09,10,11] Class Members & their Accessing
oopusingc.pptx
oops-1
Unit i
Oop objects_classes
cpp class unitdfdsfasadfsdASsASass 4.ppt
Lab 4 (1).pdf
classandobjectunit2-150824133722-lva1-app6891.ppt
Chapter18 class-and-objects
Object Oriented Programming Examples with explanation

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Programs and apps: productivity, graphics, security and other tools
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Big Data Technologies - Introduction.pptx
Understanding_Digital_Forensics_Presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...

Classes and objects

  • 1. Name – Rajveer Kaur Section – N2 Roll No. - 115312
  • 2.  Classes in C++  Objects  Creating an object of class  Special member functions  Implementing class methods  Accessing class members  Destructors  Instance variable methods  Class abstraction
  • 3. 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 Any valid { identifier …. …. Class body (data member + …. methods) };
  • 4. Objects • Objects have three responsibilities:  What they know about themselves – (e.g., Attributes)  What they do – (e.g., Operations) What they know about other objects – (e.g., Relationships) 4
  • 5. Defining Class A CLASS is a template (specification, blueprint) for a collection of objects that share a common set of attributes and operations. HealthClubMember attributes Class operations Objects 5
  • 6.  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.
  • 7.  Thisclass example shows how we can encapsulate (gather) a circle information into one package (unit or class) No need for others classes to class Circle access and retrieve its value directly. The { class methods are responsible for private: that only. double radius; public: void setRadius(double r); They are accessible from outside double getDiameter(); the class, and they can access the double getArea(); member (radius) double getCircumference(); };
  • 8.  Declaringa 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;
  • 9.  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
  • 10. class Circle Constructor with no { argument private: double radius; public: Constructor with one Circle(); argument Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); };
  • 11. 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( ){ … }
  • 12. 2. Member functions defined inside class  Do not need scope resolution operator, class name; class Circle { Defined private: inside double radius; class public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); };
  • 13. 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); }
  • 14.  Operators to access class members  Identical to those for structs  Dot member selection operator (.)  Object  Reference to object  Arrow member selection operator (->)  Pointers
  • 15.  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
  • 16.  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;} Destructor int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); };
  • 17. Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.
  • 18. Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class.
  • 19.  The scope of instance and class variables is the entire class. They can be declared anywhere inside a class.  The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.
  • 20.  Use this to refer to the current object.  Usethis to invoke other constructors of the object.
  • 21. Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.