ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
1. ECAP444 - OBJECT ORIENTED
PROGRAMMING USING C++
Dr. V. DEVENDRAN
Professor
UNIT - 1
2. Object Oriented Programming in C++
• Object oriented programming is a type of
programming which uses objects and classes its
functioning.
• The object-oriented programming is based on real
world entities like inheritance, polymorphism, data
hiding, etc.
4. Class
• Class is basic structure block on object oriented
programming.
• A class is a data-type that has its own members i.e.
data members and member functions.
• Members of class can accessed by making
instance of class
5. Properties of class
• Class is a user-defined data-type.
• Data members are variables of the class.
• Member functions are the methods that are used to
manipulate data members.
Data members define the properties of the class where
as member function defines the behaviour on class.
6. Syntax of class
class class_name
{
data_type data_name;
return_type method_name(parameters);
}
7. Object
• An object is instance of class.
• An object is entity that is created to allocate
memory.
• An object is entity access data member and
member functions of class in object oriented
programming.
10. Encapsulation
• Encapsulation is binding together the data and
related function that can manipulate the data.
• Encapsulation is the concept of wrapping together
of data and information in a single unit.
11. Polymorphism
The name defines polymorphism is multiple forms.
which means polymorphism is the ability of object
oriented programming to do some work using
multiple forms.
12. Inheritance
• The capability of a class to derive properties and
characteristics from another class is
called Inheritance.
• The class that derives properties from other class is
known as child class or subclass and the class
from which the properties are inherited is base
class or parent class.
13. Inheritance
Child Class or Sub Class: The class that inherits
properties from another class is called Sub class or
Derived Class.
Parent Class or Super Class: The class whose
properties are inherited by sub class is called Base
Class or Super class.
14. Types of Inheritance
• Single inheritance
• Multiple inheritance
• Multi level inheritance
• Hierarchical inheritance
• Hybrid inheritance
15. ABSTRACTION
• Abstraction is concept of hiding data and showing
only relevant data to the final user.
• There are two ways which can accomplish data
abstraction
• Using classes
• Using header file
17. Benefits of OOP's
• Modular approach
• Reusability of code
• Flexibility
• It is very easy to partition the work in a project
based on objects.
• It is possible to map the objects in problem domain
to those in the program.
18. Benefits of OOP's
• Object oriented systems can be easily upgraded
from small to large systems.
• Software complexity can be easily managed.
• OOP provides a good framework for code libraries
where supplied software components can be easily
adapted and modified by the programmer. This is
particularly useful for developing graphical user
interfaces.
19. Benefits of OOP's
• OOP makes it easy to maintain and modify existing
code as new objects can be created with small
differences to existing ones.
20. C++ Class
• A class is a blueprint for the object.
• Keyword class is used to create class.
• The class body, enclosed by a pair of curly braces.
22. C++ Access Specifier
• C++ access specifiers are used for determining or
setting the boundary for the availability of class
members (data members and member functions)
beyond that class.
• The class members are grouped into sections,
private protected and public. These keywords are
called access specifiers
23. Types of C++ Access Modifiers
• public
• private
• Protected
By default the class members are private.
24. public access specifier
• The public keyword is used to create public
members (data and functions).
• The public members are accessible from any part
of the program.
25. Example : C++ public access specifier
#include<iostream>
using namespace std;
class abc{
public:
int number;
void display(){ cout<<"Number ="<<number; } };
main() {
abc obj1;
cout<<"Enter number";
cin>>obj1.number;
obj1.display(); }
26. private access specifier
• The private keyword is used to create private
members (data and functions).
• The private members can only be accessed from
within the class.
• But, friend classes and friend functions can access
private members.
27. Example : C++ private access specifier
#include<iostream> using namespace std;
class abc {
private: int number;
public:
void display(int n) {
number=n; cout<<"Number ="<<number; } };
main() {
int input_number; abc obj1; cout<<"Enter number”; cin>>input_number;
obj1.display(input_number); }
28. protected access specifier
• The protected keyword is used to create protected
members (data and function).
• The protected members can be accessed within the
class and from the derived class.
Protected access specifier accessed using
inheritance in C++.
29. C++ Class Member Function
• Member function of a class is a function that has its
definition or its prototype within the class definition like any
other variable.
• It operates on any object of the class.
• A member function is defined outside the class using the ::
(double colon symbol) scope resolution operator. This is
useful when we did not want to define the function within
the main program, which makes the program more
understandable and easy to maintain.
31. Program
#include<iostream>
using namespace std;
class find_sum{
public:
int x,y; int sum();
};
int find_sum ::sum(){
return x+y; }
int main(){
find_sum sum1;
sum1.x=100;
sum1.y=200;
cout<<"Sum of x and y
is"<<sum1.sum();
return 0;
}
32. Nested member function
• A member function may call another member function within
itself. This is called nesting of member functions.
• A member function can access not only the public functions
but also the private functions of the class it belongs to.
• Calling a member function with in another member
function.
• Scope resolution operators ( : : ) used to defining a function
outside a class.
33. Program
#include<iostream>
using namespace std;
class area {
private:
int l,w;
public:
void getdata();
void display_area(); };
void area::getdata() {
cout<<"Enter length and width";
cin>>l>>w; }
void area::display_area() {
getdata();
cout<<"area is"<<l*w; }
int main(){
area a1;
a1.display_area(); }
34. Private Member Function
• A function declared inside the class's private
section is known as "private member function". A
private member function is accessible through the
only public member function.
• A private member variable or function cannot be
accessed, or even viewed from outside the class.
Only the class and friend functions can access
private members.
35. Array
An array is a collection of elements of the same type
placed in contiguous memory .
Syntax
type arrayName [ arraySize ];
36. Program
#include<iostream>
using namespace std;
int main(){
int arr[5];
cout<<"Enter Array Elements";
for(int i=0;i<=4;i++) {
cin>>arr[i];
}
}
cout << "Entered array elements are” <<
endl;
for(int i=0;i<=4;i++) {
cout<<arr[i]<<endl;
}
return 0;
}
37. Array within a class
• Arrays can be declared as the members of a class.
The arrays can be declared as private, public or
protected members of the class.
38. Program
#include<iostream>
using namespace std;
class student {
int marks[5];
public:
void getdata ();
void showdata();
};
void student::getdata() {
for(int i=0;i<=4;i++) {
cin>>marks[i]; } }
void student::showdata() {
for(int i=0;i<=4;i++) {
cout<<marks[i]<<endl; } }
int main() {
student stu; stu.getdata();
stu.showdata(); }