L5b OO-in-C++
L5b OO-in-C++
Lecture 05b:
Object-Oriented
in C++
1
1
Topics
Analysis of Class Concept
Class in C++
Member Functions
Constructor and Destructors
Member Function (Methods)
Access Control Rules
Friend Function
Derived Class in C++
Full Example Program:Constructor & Destructor
Analysis of Inheritance Concepts
Inheritance in C++
Full Example Program: Inheritance
Full Example Program: Polymorphism 2
Analysis of Class Concept
Class facilitates the classification of objects with similar
properties, and sub-classing can be employed to specify
specialization of the general properties.
4
Class in C++
The syntax for class specification as
follows:
class class_name {
declaration of the ??? members of
the class
public:
declarations of the ??? members of
the class
}
5
Class in C++
The syntax for class specification as follows:
class class_name {
declaration of the private members of the class
public:
declarations of the public members of the class
}
Constructor function
Class in C++ (con’t)
Consider the following example:
class square {
int side;
int x,y;
public:
void square (int);
void move(int, int);
void display();
}
The operation which can be performed on instance of the class square
are move and display.
The private section of the class square contains 3 variables:
side of the square and the position in term of x and y coordinates.
Constructor function
First member function defined in the public section.
Same name as the name of the class. (e.g., void square(int))
8
Squaresize4.move(10,8);
Class in C++ (con’t)
A class square, circle, rectangles, and other geometric objects have
different shapes, they can be viewed as belonging to a class of ‘generic
shapes’ or ‘shapes’.
These shapes (square, circle, etc) are instances of class ‘shapes’.
All instances of class ‘shapes’ have some common operations that can
be performed on shapes (e.g., move & display)
Thus, the class shapes can be defined as follows:
class shapes {
int x,y;
public:
void move (int, int);
void display();
}
10
the specific instances of class shapes (square, circle, rectangles, etc) can
be derived from the generic class of shapes.
Constructor and Destructors
When object is created, a constructor function for
that object is invoked, if an appropriate
constructor function is declared in the class. It has
the same name as that of the class.
The constructor for base class and the derived class
can be implemented as shown in the example
below.
employee::employee(String e_name) {
name = e_name;
}
manager::manager(String m_name) :
employee(m_name) {
}
11
Constructor and Destructors (con’t)
When the instance objects are no longer needed
they can be destroyed to release memory occupied
by using special kind of a member function called
destructors (opposite of constructor function)
The name of destructors function is the name of
the class preceded by a tilde (~) character.
Example, the destructors for class ‘manager’ as
follow:
manager::~ manager()
12
Member Functions (Methods)
Member function for the base class & the derived class can be
implemented as shown in the following examples:
void employee::set_salary(int sal) {
salary = sal;
tax = (18/100) * sal;
}
class employee {
String name;
String id;
protected:
int salary;
int tax;
public:
employee(String e_name); /* constructor function */
employee(String m_name, String e_id);
set_emplname(String e_name);
void set_salary(int sal);
void set id(String e_id); 15
}
Access Control Rules
16
Access Control Rules(cont..)
Several rules govern the access control mechanism:
1.A protected member of a base class is a
protected member of the derived class if the
derivation is public.
2.A protected member of a base class is a
private member of derived class if the
derivation is private.
3.When the base class is used as a private base
class, all its members are considered to be
private members of the derived class.
4.A member of a derived class or a friend of a
derived class has access only to protected
and public members of the base class.
17
Friend Functions
18
Friend Functions (con’t)
#include <String.h>
#include <iostream.h>
class employee {
char name;
char id;
protected:
int salary;
int tax;
public:
employee(string e_name); /* constructor function */
void set_salary(int sal);
friend void display_emp_info(employee &);
};
void display_emp_info(employee & emp) {
cout << " Employee : " << emp.name<< "\n";
cout << " ID : " << emp.id;
}
19
Full Example program: Constructor & Destructor
Let us declare a class Point as: // constructor implementation without
// parameter
//This program declares a class Point Point::Point() {
// for variables x and y // initialize the private data to zero
#include <iostream.h> x = 0; y = 0;
};
class Point{
private: //constructor implementation with
int x, y; // point coordinate // parameters
public: Point::Point(int val1, int val2) {
Point(); // constructor //initialize the private data to user data
// declaration without parameter setX(val1); setY(val2);
Point(int val1, int val2); };
// constructor declaration with
// parameters //destructor implementation
~Point(); // destructor Point::~Point(){
// declaration //print the message to indicate the
void setX(int val); // deletion of an object
void setY(int val); cout << "The object has been
int getX(); deleted.\n";
int getY(); };
}; // class Point
20
void Point::setX(int val){
x = val;
};
int Point::getX() {
return x;
};
int Point::getY() {
return y;
};
21
The driver for the class Point
//This program serves as a driver (main) // create a new instance of a class
for class Point. // Point via a constructor with
//It instantiates the class Point and call // initialization of the user numbers
methods from class Point
Point bpoint(4,5);
#include <iostream.h> cout << "\nThe numbers from the
#include "Ptwc.cpp" Point object are x = " <<
int main() { bpoint.getX();
// create a new instance of a class Point cout << " and y = " <<
bpoint.getY()<< "\n";
// via a constructor with initialization
cout << "\n";
// of the class numbers.
Point apoint;
return 0;
cout << "\nThe numbers from the Point
object are x = " << apoint.getX(); };
cout << " and y = " << apoint.getY()<<
"\n";
cout << "\n"; 22
23
Constructor & Destructor: Few Points to
Note
Two ways of creating an object via
constructor methods
The first case, “Point apoint;”, instantiates a
class Point without passing parameters. This
will automatically initialize the data of an
object (variables x and y are assigned to zero).
The second case, “Point bpoint(4,5);”,
instantiates a class Point with parameters
(values 4 and 5). This will automatically
initialize the data of an object to user’ defined
values (variables x and y are assigned to 4 and
5, respectively). 24
Constructor & Destructor:
Few Points to Note (cont…)
The destructor method is not called in program. The
C++ compiler will automatically call the destructor
method before the program terminates.
Since the objects are created twice, the compiler
calls the destructor method two times in order to
delete the two created objects.
The destructor method is a simple method with a
print statement. For this reason, the destructor is
usually not declared and used in a class. The
destructor is important when dynamic allocation
storage is used (for example, pointers).
25
Analysis of Inheritance
Concept
Inheritance has been used in OO paradigm
as a mechanism for creating new classes
from existing class. Thus the hierarchy of
classes is created where the subclasses Employee
name : string
are derived from superclasses. salary : integer
IC_No : string
32
The driver of the class Circle
//This program serves as a driver (main)
// for class Circle.
//It instantiates the class Circle and call
// method from class Circle
//which will indirectly call methods from
// class Point
#include "Circle.cpp"
int main()
{
Circle circle;
circle.GetPoint();
circle.GetResult();
return 0;
};
33
34
Inheritance: Few Points to
Note
Since inheritance means the subclass will inherit all the
data and methods from the superclass, the declaration of
class Circle above will automatically make the methods
from class Point available in Class Circle.
Since the data from class Point is declared private, it is not
accessible in class Circle. The data can only be accessed
through methods getX() and getY(). If class Circle wants to
access directly the data, the type in class Point needs to
be changed to protected. This will allow the subclass to
access the data from the superclass.
Class Circle inherits all the methods from class Point and at
the same time introduces one data (Result of type float)
and two new methods: GetPoint() and GetResult().
35
Polymorphism
36
Polymorphism: Few Points to Note
38
Another Full Example program: Polymorphism
Let us declare a class Circle as:
//Filename: CirPoly.cpp
//This program declares a class Circle based on
void Circle::GetResult() {
//derived class Point
//It illustrates Polymorphism in C++ const float Pi = 3.14151296;
#include <iostream.h> Result = 2 * Pi * getX();
#include "PtPoly.cpp" cout << "\nThe circumference of a
circle from center point 0 to ";
class Circle: public Point cout << getX() << " is "<< Result
{ private : << "\n";;
float Result; };
public:
void SetPoint(); void Circle::DisplayDetails() {
void GetResult(); // display output via methods getX and //getY
void DisplayDetails(); cout << "\nThe coordinates from the
}; Circle object are x = " << getX();
cout << " and y = " << getY()<< "\n";
void Circle::SetPoint(){
int x, y;
cout << '\n';
cout << "Type coordinate x: "; };
cin >> x;
cout << "Type coordinate y: ";
cin >> y;
setX(x); setY(y);
}; 39
The driver of the class Circle
//Filename: PolyDr.cpp
//This program serves as a driver (main) for
// class Point and Circle.
// It illustrates Polymorphism in C++
#include "CirPoly.cpp"
int main() {
Point apoint;
apoint.setX(4); apoint.setY(5);
Circle circle;
circle.SetPoint();
circle.GetResult();
apoint.DisplayDetails();
circle.DisplayDetails();
return 0;
};
40
41
Summary
OO in C++
Constructors and Destructors
Member functions
Friend functions
Inheritance
Polymorphism
42
Questions???
?
43