KEMBAR78
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
ECAP444 - OBJECT ORIENTED
PROGRAMMING USING C++
Dr. V. DEVENDRAN
Professor
UNIT - 1
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.
Object Oriented Programming in C++
OOPS
Encapsulation
Inheritance
Abstraction
Polymorphism
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
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.
Syntax of class
class class_name
{
data_type data_name;
return_type method_name(parameters);
}
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.
Syntax
class_name object_name;
Example
#include<iostream>
using namespace std;
class demo
{
public:
int f_num, s_num;
sum ( int a, int b ) {
cout << a+b; }
};
Int main()
{
demo d1;
d1.sum(d1.f_num=10,d1.s_num=39);
return 0;
}
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.
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.
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.
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.
Types of Inheritance
• Single inheritance
• Multiple inheritance
• Multi level inheritance
• Hierarchical inheritance
• Hybrid inheritance
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
Object Oriented Programming Languages
• Java
• JavaScript
• Python
• C++
• Visual Basic .NET
• Ruby
• Scala
• PHP
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.
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.
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.
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.
Syntax
class className {
access specifier;
//data members;
//member functions;
};
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
Types of C++ Access Modifiers
• public
• private
• Protected
By default the class members are private.
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.
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(); }
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.
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); }
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++.
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.
Syntax
return_type class_name :: member_function
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;
}
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.
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(); }
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.
Array
An array is a collection of elements of the same type
placed in contiguous memory .
Syntax
type arrayName [ arraySize ];
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;
}
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.
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(); }

ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx

  • 1.
    ECAP444 - OBJECTORIENTED PROGRAMMING USING C++ Dr. V. DEVENDRAN Professor UNIT - 1
  • 2.
    Object Oriented Programmingin 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.
  • 3.
    Object Oriented Programmingin C++ OOPS Encapsulation Inheritance Abstraction Polymorphism
  • 4.
    Class • Class isbasic 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 classclass_name { data_type data_name; return_type method_name(parameters); }
  • 7.
    Object • An objectis 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.
  • 8.
  • 9.
    Example #include<iostream> using namespace std; classdemo { public: int f_num, s_num; sum ( int a, int b ) { cout << a+b; } }; Int main() { demo d1; d1.sum(d1.f_num=10,d1.s_num=39); return 0; }
  • 10.
    Encapsulation • Encapsulation isbinding 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 definespolymorphism is multiple forms. which means polymorphism is the ability of object oriented programming to do some work using multiple forms.
  • 12.
    Inheritance • The capabilityof 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 orSub 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 isconcept 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
  • 16.
    Object Oriented ProgrammingLanguages • Java • JavaScript • Python • C++ • Visual Basic .NET • Ruby • Scala • PHP
  • 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 • Aclass is a blueprint for the object. • Keyword class is used to create class. • The class body, enclosed by a pair of curly braces.
  • 21.
    Syntax class className { accessspecifier; //data members; //member functions; };
  • 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 MemberFunction • 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.
  • 30.
  • 31.
    Program #include<iostream> using namespace std; classfind_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; classarea { 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 isa collection of elements of the same type placed in contiguous memory . Syntax type arrayName [ arraySize ];
  • 36.
    Program #include<iostream> using namespace std; intmain(){ 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 aclass • 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; classstudent { 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(); }