SlideShare una empresa de Scribd logo
1 de 22
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Friend function
Friend functionFriend function
Friend function
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Templates
TemplatesTemplates
Templates
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 

Destacado

Inheritance concepts
Inheritance concepts Inheritance concepts
Inheritance concepts Kumar
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overridingPinky Anaya
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsAnil Kumar
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael Heron
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recoveryAnne Lee
 
Functions
FunctionsFunctions
FunctionsOnline
 
Backup and recovery in oracle
Backup and recovery in oracleBackup and recovery in oracle
Backup and recovery in oraclesadegh salehi
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Database recovery techniques
Database recovery techniquesDatabase recovery techniques
Database recovery techniquespusp220
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 

Destacado (20)

Inheritance concepts
Inheritance concepts Inheritance concepts
Inheritance concepts
 
Unit07 dbms
Unit07 dbmsUnit07 dbms
Unit07 dbms
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Data recovery
Data recoveryData recovery
Data recovery
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recovery
 
Functions
FunctionsFunctions
Functions
 
Backup and recovery in oracle
Backup and recovery in oracleBackup and recovery in oracle
Backup and recovery in oracle
 
Chapter19
Chapter19Chapter19
Chapter19
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Database recovery techniques
Database recovery techniquesDatabase recovery techniques
Database recovery techniques
 
Data recovery
Data recoveryData recovery
Data recovery
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Database recovery
Database recoveryDatabase recovery
Database recovery
 
Data recovery
Data recoveryData recovery
Data recovery
 

Similar a Classes and objects

Similar a Classes and objects (20)

C++ classes
C++ classesC++ classes
C++ classes
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Oo ps
Oo psOo ps
Oo ps
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
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
 
[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
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ classes
C++ classesC++ classes
C++ classes
 
C++ Classes Tutorials.ppt
C++ Classes Tutorials.pptC++ Classes Tutorials.ppt
C++ Classes Tutorials.ppt
 
oops-1
oops-1oops-1
oops-1
 
Unit i
Unit iUnit i
Unit i
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Classes
ClassesClasses
Classes
 
Class and object
Class and objectClass and object
Class and object
 

Último

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Último (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

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.