Download Object Oriented Programming-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!
Outlines
Object Oriented Programming
Classes
Data Members and Member Functions
Access Specifier
Special Member Functions
Example of Class
Why OOP?
Summary
Object Oriented Programming
Programmer thinks about and defines the
attributes and behavior of objects.
Often the objects are modeled after real-
world entities.
Classes
Class has data members and functions
Data members are usually used only inside
the class and cannot be access from outside
Member functions are can also be accessed
in main
But this is not a rule ,sometimes opposite to it
may be the case
Member Access specifier
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
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.
Object
Any physical thing that can be felt or seen
A class is a type, and an object of this class is
just a variable.
Also called Instance
Distinction is that object is only definition of a
thing whereas instance is actual manipulation
Special Member Functions
Constructor:
– Public function member
– called when a new object is created
(instantiated).
– Initialize data members.
– Same name as class
– No return type
– Several constructors
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
Implementing class Functions
2. 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
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);
Defined outside class
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);
void main() { Circle c1,c2(7);
cout<<“The area of c 1 :” <<c1.getArea()<<“\n”;
//c1.raduis = 5;//syntax error c1.setRadius(5);
cout<<“The circumference of c 1 :” << c1.getCircumference()<<“\n”;
cout<<“The Diameter of c 2 :” <<c2.getDiameter()<<“\n”; }
The first
constructor is
called
The second
constructor is
called
Since radius is a
private class data
member
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);
void main() { Circle c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7);
cout<<“The are of cp 2 :” <<cp2->getArea();
}
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
**Time::Time() { hour = new int; minute = new int; second = new int; *hour = minute = second = 0 ; }
**Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; *hour = h; minute = m; second = s; }
**void Time::setTime(int h,int m,int s) { *hour = h; minute = m; second = s; }
Dynamic locations should be allocated to pointers first