0% found this document useful (0 votes)
12 views18 pages

2 Classes

Uploaded by

kaled20188a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

2 Classes

Uploaded by

kaled20188a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

C++ Object Oriented

Programming
Object Oriented Programming
 Programmer thinks about and defines the
attributes and behavior of objects.
 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)
struct versus class
In C++ struct and class can be used 
interchangeably to create a class with one
exception
What if we forget to put an access modifier before 
?the first field
{ struct Robot { OR class Robot
;float locX; float locX
In a class, until an access modifer is supplied, the fields
are assumed to be private
In a struct, the fields are assumed to be public
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 Any valid
{ identifier
.…
.…
Class body (data member
.…
+ methods)
methods
;}
++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 private members or
… methods


public:
… Public members or methods


;}
Classes in C++
 Member access specifiers

– public:
 can be accessed outside the class directly.

– 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)
No need for others classes to
class Circle access and retrieve its value
directly. The class methods are
{
responsible for that only.
private:
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 getPerimeter();
};
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).

 Once an object of a class is declared, a new


memory location is created for it to store its data
members and code
 You can create many objects from a class type.
Special Member Functions
 Constructor:
– Public function member
– called when a new object is created.
– Initialize data members.
– Same name as class
– No return type
– Several constructors
 Function overloading
Special Member Functions

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 getPerimeter();
};
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( ){

}
Implementing class methods
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 getPerimeter();
};
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 getPerimeter();
};
Circle::Circle(int r)
{
Defined outside class
radius = r;
}
double Circle::getArea()
{
return radius * radius * (22.0/7);
}
double Circle:: getPerimeter()
{
return 2 * radius * (22.0/7);
}
Accessing Class Members

 Operators to access class members


– Dot member selection operator (.)
 Object

 Reference to object

– Arrow member selection operator (->)


 Pointers
class Circle
{
private:
double radius;
public:
The first
Circle() { radius = 0.0;} The second
constructor is
Circle(int r); constructor is
called
void setRadius(double r){radius = r;} called
double getDiameter(){ return radius *2;}
double getArea();
Since radius is a
double getPerimeter(); int main()
private class data
}; {
Circle c1,c2(7); member
Circle::Circle(int r)
{
cout<<"The area of c1:"
radius = r;
<<c1.getArea()<<"\n";
}
double Circle::getArea() //c1.raduis = 5;//syntax error
{
return radius * radius * (22.0/7); c1.setRadius(5);
} cout<<"The perimeter of c1:"
double Circle:: getPerimeter() << c1.getPerimeter()<<"\n";
{
cout<<"The Diameter of c2:"
return 2 * radius * (22.0/7);
<<c2.getDiameter()<<"\n";}
}
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 getPerimeter();
};
Circle::Circle(int r) void main()
{
{
Circle c(5);
radius = r; Circle *cp1 = &c;
} Circle *cp2 = new Circle(7);
double Circle::getArea() cout<<"The are of cp1: "
{ <<cp1->getArea()<<endl;
return radius * radius * (22.0/7); cout<<"The are of cp2: "
} <<cp2->getArea()<<endl;
double Circle:: getPerimeter() }
{
return 2 * radius * (22.0/7);
}
Destructors
 Destructors
– Special member function
– Same name as class
 Preceded with tilde (~)
– No arguments
– No return value
– Cannot be overloaded
– Destructors are usually used to deallocate memory
 Reuse memory for new objects
 Mainly used to de-allocate dynamic memory locations

You might also like