C++ OOP concepts explained including classes, objects, constructors and destructors
1. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
OPPS using
C++ BCA – III
Paper Code 209
Unit-1
2. Object oriented programming
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
• Object Oriented Programming (OOP) is an
approach to program organization and
development that attempts to eliminate some
of the pitfalls of conventional programming
methods by incorporating the best of
structured programming features with several
powerful new concepts.
3. Procedure-Oriented Programming
Procedure oriented programming basically
consists of writing a list of instructions for the
computer to follow, and organizing these
instructions into groups known as functions.
We normally use flowcharts to organize these
actions and represent the flow of control from
one action to another
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
4. Characteristics of POP
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs
known as functions.
• Most of the functions share global data.
• Data move openly around the system from function
to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
5. Characteristics of OOPS
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are ties together in the
data structure.
• Data is hidden and cannot be accessed by external function.
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
6. Features of OOPs
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
7. Features of OOPs
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
8. Introduction of C++
• What is C++?
• Applications of C++.
• Simple Program Features.
• Comments
• Output Operator
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
9. • Header Files
• Return Type of main
• More C++ Statements
• An example With Class
• Structure of C++ Program
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
10. • It is an object oriented programming.
• It is the super set of c.
• It has bottom-up approach.
• Applications:-
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
11. •#include< iostream.h >
#include< conio.h >
int main( )
{
cout<<
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
12. • Input & Output Operator
• The operator >> is called the extraction
operator. It is used for output.
• The operator << is known as insertion
operator. It is used for input .
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
13. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
OPPS using
C++ BCA – III
Paper Code 209
Unit-2
14. Definition and declaration of class
• Class Tag Name
• Data member
• Member Function Program Access Level
a) Private
b) Public
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
15. Class Definition
A class in C++ combines related data and functions
together. It makes a data type which is used for
creating objects of this type. Classes represent real
world entities that have both data type properties
(characteristics) and associated operations
(behavior).
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
16. Class structure
• Class name_of _class
• { private : variable declaration;
// data member Function declaration;
// Member Function (Method) protected:
Variable declaration;
Function declaration; public
: variable declaration;
Function declaration; };
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
17. 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.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
18. Creating Object
• 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;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
19. 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
• Function overloading
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
20. • class Circle
• {
• private:
• double radius;
• public:
• Circle();
• Circle(int r);
• void setRadius(double r);
• double getDiameter();
• double getArea();
• double getCircumference();
• };
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
21. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
OPPS using
C++ BCA – III
Paper Code 209
Unit-3
22. Destructors
It is an object oriented programming.
• It is the super set of c.
• It has bottom-up approach.
• Applications:- Destructors
– Special member function
– Same name as class
• Preceded with tilde (~)
– No arguments
– No return value
– Cannot be overloaded
– Before system reclaims object’s memory
• Reuse memory for new objects
• Mainly used to de-allocate dynamic memory locations
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
23. • STATIC CLASS MEMBERS
Data members and member functions of a
class in C++, may be qualified as static. We can
have static data members and static member
function in a class.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
24. • A friend function of a class is defined outside
that class' scope but it has the right to access
all private and protected members of the
class. Even though the prototypes for friend
functions appear in the class
definition, friendsare not member functions.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
25. Inline functions
• An inline functions to reduce the function call
overhead. Inline function is a function that is
expanded in line when it is called. When the
inline function is called whole code of the
inline function gets inserted or substituted at
the point of inline function call. This
substitution is performed by the C++ compiler
at compile time. Inline function may increase
efficiency if it is small.
26. • inline return-type function-name(parameters)
{ // function code }
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
27. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
OPPS using
C++ BCA – III
Paper Code 209
Unit-4
28. Templates
• Type-independent patterns that can
work with multiple data types.
– Generic programming
– Code reusable
• Function Templates
– These define logic behind the algorithms
that work for multiple data types.
• Class Templates
–Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIndia)
29. Function and function templates
}
• C++ routines work on specific types. We
often need to write different routines to
perform the same operation on different
data types.
int maximum(int a, int b, int c)
{
int max = a;
if (b > max) max = b; if (c >
max) max = c;
return max;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In2d6ia)
30. Function and function templates
float maximum(float a, float b, float c)
{
float max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In2d7ia)
31. Function and function templates
double maximum(double a,
double b, double c)
{
double max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In2d8ia)
32. Function Templates
• Generic function to find a maximum value
(see maximum example).
Template <class T>
T maximum(T a, T b, T c)
{
T max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In2d9ia)
33. Function Templates Usage
• After a function template is included (or defined),
the function can be used by passing parameters of
real types.
Template <class T>
T maximum(T a, T b, T c)
…
int i1, i2, i3;
…
Int m = maximum(i1, i2,i3);
• maximum(i1, i2, i3) will invoke the template function with T==int. The function
returns a value of int type.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council ofIn3d0ia)
34. Function Templates Usage
• Each call to maximum() on a different data type
forces the compiler to generate a different
function using the template. See the maximum
example.
– One copy of code for many types.
int i1, i2, i3;
// invoke int version of maximum
cout << "The maximum integer value is: "
<< maximum( i1, i2, i3 );
// demonstrate maximum with double values
double d1, d2, d3;
// invoke double version of maximum
cout << "The maximum double value is: "
<< maximum( d1, d2, d3 );
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In3d1ia)
35. Another example
template< class T >
void printArray( const T *array, const int count )
{
for ( int i = 0; i < count; i++ )
cout << array[ i ] << " "; cout << endl;
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In3d2ia)
36. Class template
• So far the classes that we define use fix data
types.
• Sometime is useful to allow storage in a class
for different data types.
• See simplelist1 (a list of int type elements)
example
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In3d1ia)
37. Class template
– Function templates allow writing generic functions
that work on many types.
– Same idea applies to defining generic classes that
work with many types -- extract the type to be a
template to make a generic classes.
– See simplelist3
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In3d1ia)
38. Class template
• To make a class into a template, prefix the class definition with the syntax:
template< class T >
– Here T is just a type parameter. Like a function parameter, it is a
place holder.
– When the class is instantiated, T is replaced by a real type.
• To access a member function, use the following syntax:
– className< T >:: memberName.
• Usi
–
– SimpleList < T > :: SimpleList()
Chanderprabhu Jain College of Higher Studies & School of
Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council
of In d ia)
39. Exception handling
• Exceptions
– Indicate problems that occur during a program’s
execution
– Occur infrequently
• Exception handling
– Can resolve exceptions
• Allow a program to continue executing or
• Notify the user of the problem and
• Terminate the program in a controlled manner
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi –110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of In3d1ia)