Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Outline
Basics of Object Oriented Programming
Introduction to C++
C++ Language OOP Constructs & Features
What is OOP?
Object Oriented Programming (OOP) is the programming
paradigm uses objects to design applications and computer
programs.
Object oriented programming provides some nice features, some
of them include :
Classes
Data Abstraction
Objects
Data Encapsulation
Polymorphism
Inheritance
Overloading
Objects
The basic unit of Object Oriented Programming.
Objects are data structures consisting of data fields and methods.
Each object is a particular instance of a class, which has its
unique name.
Each object carries the properties of its relevant class, i.e.variables & associated functions.
When created, each object allocates memory for variables and
data associated with it.
Classes
Classes are the data types from which objects are created.
It can be seen as a template which define the abstract
characteristics and behaviour of a thing (object).
Properties Characteristics of each object that may be created
from the class.
Methods Actions to be performed by the objects that are
declared in class.
No memory is allocated when a class is created.
Classes (Contd..)
Memory allocation happens only when the actual instances of a
class (the objects) are created.
Basically, a class is an expanded concept of a data structure;
which can hold data as well as functions.
In terms of variables, a class would be the data_type and the
object would be the variable.
Introduction to C++
Introduction to C++
C++ - An enhancement of C with added OOP concepts.
Originally named C with Classes. Later changed to C++ by
adding increment operator of C language.
Standardized
14882:2003.
as
ISO/IEC
class class_name{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Employer is a class
emp is an object of
class Employer
Class has data and
member functions that
may be accessed through
a . (dot) operator.
class Example
class
#include <iostream>
using namespace std;
class cube
{
public:
double side;
double volume()
{
return (side * side* side);
}
};
main() Program
int main()
{
double volume1 = 0;
cube c1,c2;
cout << "Enter the length of the cube:" << endl;
cin >> c1.side;
cout << "The volume of the cube is:"
<< c1.volume() << endl;
c2.side = c1.side + 2;
cout << "The volume of the 2 nd cube is:
<< c2.volume() << endl;
return(0);
}
Constructor
Constructors are used to initialize the objects.
Constructor A special kind of function which is the member of
the class.
The name of the constructor is same as the name of the class.
Constructor is automatically called whenever an object is
created.
Constructors DO NOT have a return value.
Constructor (Contd..)
A default constructor is the one with no parameters.
If no constructor is defined by the user, the compiler supplies the
default constructor.
Destructor
A destructor is the complement of the constructor that is, it is
used to destroy the objects.
The objects are destroyed in order to deallocate the memory
occupied.
The name of the destructor is the same as the constructor and is
preceded by a ~ (tilt) operator.
A destructor for objects is executed in the reverse order of the
constructor functions.
Constructor-Destructor Example
Constructor & Destructor
#include<iostream>
using namespace std;
//
continues here...
class cube
cube()
{
{
public:
cout << "A default constructor
double side;
is called " << endl;
double volume()
}
{
~cube()
return( side * side * side );
{
}
cout << "Destructing " <<
cube(double side1)
side << endl;
{
}
cout << "A constructor is called" << endl; };
side=side1;
}
// continue...
main Program
cout << "The side of the cube is: " << c1.side << endl;
cout << "The volume of the first cube is : " << c1.volume() << endl;
cout << "Enter the length of the second cube : " ;
cin >> c2.side;
cout << "The volume of second cube is : " << c2.volume() << endl;
return(0);
}
// end...
#include<iostream>
using namespace std;
cube()
class cube
{
{
cout << "A default constructor
public:
is called " << endl;
double side;
}
double volume()
~cube()
{
{
return( side * side * side );
cout << "Destructing " <<
}
side << endl;
cube(double side1)
}
{
};
cout << "A constructor is called" << endl;
side=side1;
}
// continue..
main Program
cout << "The side of the cube is: " << c1.side << endl;
cout << "The volume of the first cube is : " << c1.volume() << endl;
cout << "Enter the length of the second cube : " ;
cin >> c2.side;
cout << "The volume of second cube is : " << c2.volume() << endl;
return(0);
}
// end...
Inheritance
A process of forming a new class from an existing class (often
called base class).
The base class also called parent class/super class.
The derived class also called as child class/sub class/extended
class.
Inheritance helps reduce the overall size of the program code,
which helps the overall performance.
The derived/extended classes have all the properties of the base
class. Upon them, a programmer can choose to add new features
specific to the newly created derived class.
Inheritance (Contd..)
Advantages of inheritance:
Reusability
Inheritance helps the reuse of the same code .
Once the base class is defined and worked out, it need not be reworked.
Any no. of derived classes can be created from the base class as needed
while adding specific features to each derived class as needed.
Inheritance (Contd..)
Inheritance declaration:
class <derived_class_name> : <access_specifier><base_class_name>
e.g.
Inheritance (Contd..)
//base class
class food
{
public:
food(void) { x=0; }
void f(int n1)
{
x= n1*5;
}
void output(void)
{ cout << x; }
private:
int x;
};
//end of base class
Inheritance Example
// derived class
class bread: public food
{
public:
bread(void) { s1=0; }
void f1(int n1)
{
s1=n1*10;
}
void output(void)
{
food :: output();
cout << s1;
}
private:
int s1;
}
// end of derived class
// main program
int main (void)
{
bread s;
s.f(10);
s.output();
s.f1(50);
s.output();
}
//end of main program
Inheritance (Contd..)
Some important points regarding inheritance:
Derived class CANNOT have access to the constructor or destructors of the
base class.
However, it can add new member functions or variables, new constructors or
destructors.
For each object of the derived class, the memory is allocated for the data
members of the base class as well as that of the derived class.
The derived class will have access to all the member data and functions of
the base class which are not defined as private.
Data Abstraction
It allows creation of user-defined data types. It represents the
needed information without presenting the details.
The main idea behind data abstraction is to give a clear
separation between properties of data type and the associated
implementation details.
Abstract data type:
Properties clearly visible to the user interface.
Implementation details hidden.
Functional Abstraction
Data Abstraction
Encapsulation
The process of combining or packaging data with functions and
thereby allowing users to create a new abstract data type. (which
is nothing but a class!)
As data and functions reside inside a single autonomous unit.
The user cannot directly access the encapsulated data.
Encapsulation introduces the concept if information hiding.
Encapsulation (Contd..)
Encapsulation Example
class Exforsys
{
public:
int sample();
int example(char *se)
int endfunc();
//Other member functions
private:
int x;
float sq;
//Other data members
};
Encapsulation (Contd..)
As the data and the functions are bundled inside the class, the
class takes total control of maintenance and hence human errors
are reduced.
Encapsulated objects act as black boxes to the other parts of the
program. This enhances the security. And still, the objects maybe
used to achieve some functionality.
The usage of access specifiers can further add restrictions on the
way data may or may not be allowed to be accessed.
In case a user wants some non-member function to access an
objects private or protected data, he/she can use friend functions.
Abstraction
Information hiding
It hides data.
It hides implementation
Polymorphism
Polymorphism allows routines to use variables of different types
at different times.
It is an ability to use an operator or a function in different ways.
A function given different meanings or an operator functioning in
many ways is called Polymorphism.
Overloading
Overloading is a type of polymorphism.
When an exiting operator or function begins to operate on new
data type, or class, it is understood to be overloaded.
There are two types of overloading:
Function overloading, &
Operator overloading
Function Overloading
Polymorphism in which the functions assume different forms at
different times is called function overloading.
The selection of an appropriate function is done at the compile
time.
Two or more functions may have the same name but their
parameter lists should be different in terms of either:
parameters, or
their data types
int main()
{
int sal; double sal2;
employee emp1(10);
employee emp2(10,3);
cout << "Enter the no years for first employee" << endl;
cin >> emp1.year;
cout << endl << "Enter the salary per week for first employee" << endl;
cin >> sal;
cout << "The total salary of first employee is : " << emp1.calculate(sal) << endl;
cout << endl << "Enter the salary per week for second employee is : " << endl;
cin >> sal2;
cout << "The total salary of second employee is for one year:
<< emp2.calculate(sal2) << endl;
return(0);
}
// end of main program..
Operator Overloading
Polymorphism in which the operators assume different meanings
at different times is called operator overloading.
Operator functions are created to perform operator overloading.
However, operator overloading DOES NOT allow creating new
operators.
General syntax for operator function is as follows:
<return_type> operator #(<arg_list>)
{
...
}
ThanQ