classes and objects
TRANSCRIPT
![Page 1: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/1.jpg)
CLASSES AND
OBJECTSName – Rajveer Kaur
Section – N2
Roll No. - 115312
![Page 2: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/2.jpg)
CONTENTS
Classes in C++ Objects Creating an object of class Special member functions Implementing class methods Accessing class members Destructors Instance variable methods Class abstraction
![Page 3: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/3.jpg)
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
![Page 4: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/4.jpg)
4
• 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)
Objects
![Page 5: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/5.jpg)
5
Defining ClassA CLASS is a template (specification, blueprint)for a collection of objects that share a commonset of attributes and operations.
HealthClubMember
Class
Objects
attributesoperations
![Page 6: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/6.jpg)
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.
![Page 7: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/7.jpg)
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. Theclass methods are responsible forthat only.
They are accessible from outsidethe class, and they can access themember (radius)
![Page 8: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/8.jpg)
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;
![Page 9: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/9.jpg)
SPECIAL MEMBER FUNCTIONS Constructor:
Public function membercalled when a new object is created
(instantiated). Initialize data members.Same name as classNo return typeSeveral constructors
Function overloading
![Page 10: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/10.jpg)
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
![Page 11: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/11.jpg)
IMPLEMENTING CLASS METHODS 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( ){…
}
![Page 12: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/12.jpg)
IMPLEMENTING CLASS METHODS2. 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
![Page 13: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/13.jpg)
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);}
![Page 14: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/14.jpg)
ACCESSING CLASS MEMBERS Operators to access class members
Identical to those for structsDot member selection operator (.)
Object Reference to object
Arrow member selection operator (->) Pointers
![Page 15: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/15.jpg)
DESTRUCTORS Destructors
Special member functionSame name as class
Preceded with tilde (~)No arguments No return valueCannot be overloadedBefore system reclaims object’s memory
Reuse memory for new objects Mainly used to de-allocate dynamic memory
locations
![Page 16: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/16.jpg)
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
![Page 17: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/17.jpg)
INSTANCE VARIABLES, AND METHODS
Instance variables belong to a specific instance.
Instance methods are invoked by an instance of the class.
![Page 18: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/18.jpg)
CLASS VARIABLES, CONSTANTS, AND METHODS
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.
![Page 19: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/19.jpg)
SCOPE OF VARIABLES 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.
![Page 20: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/20.jpg)
THE KEYWORD THIS Use this to refer to the current object. Use this to invoke other constructors of the
object.
![Page 21: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/21.jpg)
CLASS ABSTRACTION 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.
![Page 22: Classes and objects](https://reader036.vdocument.in/reader036/viewer/2022062709/558e845f1a28abf16d8b4611/html5/thumbnails/22.jpg)
THANKS