Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Classes and objects


Published on

classes and objects

Published in: Technology, Sports
  • Be the first to comment

Classes and objects

  1. 1. Name – Rajveer KaurSection – N2Roll No. - 115312
  2. 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. 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. 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. 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 attributesClass operationsObjects 5
  6. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 17. Instance variables belong to a specificinstance.Instance methods are invoked by an instanceof the class.
  18. 18. Class variables are shared by all the instancesof the class.Class methods are not tied to a specific object.Class constants are final variables shared by allthe instances of the class.
  19. 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. 20.  Use this to refer to the current object. Usethis to invoke other constructors of the object.
  21. 21. Class abstraction means to separate classimplementation from the use of the class. Thecreator of the class provides a description of the classand let the user know how the class can be used. Theuser of the class does not need to know how the classis implemented. The detail of implementation isencapsulated and hidden from the user.
  22. 22. THANKS