Sei sulla pagina 1di 73

BASIC CONCEPTS OF

OBJECT ORIENTED
PROGRAMMING

Tuesday, February 5, 1
2019
The basics
• Introduction
• Application of C++
• Writing & executing of a c++ prg
• ANSI c++ std header files
• Comiplation & console prg’s
• Variable naming conventions
• Identifiers
• Data types
• Declaration of a variables & initialization
• Keywords
• Const keyworrd
• Setw manipulator
• Type conversion
• Type casting
• Operators & opr precedence
• Conditions constructs , user_defined dtat types
• functions

Tuesday, February 5, 2
2019
The setw manipulator
• The setw manipulator causes the number or string that follows it in the
stream to be printed within a field of n characters wide, where n is the
argument to setw(n)
• #include <iostream.h>
• #include<conio.h>
• #include<iomanip.h>
• void main()
• {
• clrscr();
• cout<<"\n\na"<<setw(8)<<"m";
• cout<<"\n\n\nInformation"<<setw(25)<<" Technology
of"<<setw(15)<<"chennai";
• getch();
• } Output

• a m
• Information Technology of chennai

Tuesday, February 5, 3
2019
Concepts of OOP
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing or (message communication)

Tuesday, February 5, 4
2019
Objects
• Objects are the basic runtime entities
• Objects contain data and code to manipulate
that data.
• Objects take up space in the memory and
have an associated address.
• When program is executed the objects
interact by sending message to one another.
• E.g., if customer and account are 2 objects –
they interact each other.

Tuesday, February 5, 5
2019
Classes
• “A class is a set of objects that share a common
structure and a common behaviour”.
• A class is a collection of objects of similar type.
• Objects are variables of the class. Once a class has
been defined, we can create any no of objects
belonging to that class.
• A class is thus a collection of objects of similar
type.
• Classes are user defined data type.
• E.g.,mango and apple are members of the class
fruit.

Tuesday, February 5, 6
2019
Class – example1-using array
• #include<iostream.h>
• #include<conio.h>
• class emp
• {
• int no;
• char name[10];
• public:
• void get()
• {
• cout<< "\n\nenter no & name \n";
• cin>>no;
• cin >>name;
• }
• void print()
• {
• cout<<"name is "<< name;
• cout<<"\nno is " << no;
• }
• };

Tuesday, February 5, 7
2019
• void main()
• {
• int i;
• emp e;
• clrscr();
• for(i=0;i<5;i++)
• {
• e.get();
• }
• for(i=0;i<5;i++)
• {
• e.print();
• }
• getch();
• }

Tuesday, February 5, 8
2019
Class example using pointer

• #include<iostream.h>
• #include<conio.h>
• class emp
• { int no;char name[10];
• public:
• void get()
• {
• cout<< "\n\nenter no & name \n";
• cin>>no;
• cin >>name;
• }
• void print()
• {
• cout<<"name is "<< name;
• cout<<"\nno is " << no;
• }
• };
Tuesday, February 5, 9
2019
• void main()
• {
• int i;
• emp *e;
• clrscr();
• for(i=0;i<5;i++)
• {
• e->get();
• e++;
• }
• e-=5;
• for(i=0;i<5;i++)
• {
• e->print();
• e++;
• }
• getch();
• }

Tuesday, February 5, 10
2019
Scope resolution operator example
• #include<iostream.h>
• class a
• {
• char name[20];
• public:
• void one();
• };
• void a::one()
• {
• cout<<"enter the name";
• cin>>name;
• cout<<"the name u have entered is"<<name;
• }
• void main()
• {
• a b;
• b.one();
• }

Tuesday, February 5, 11
2019
Data Abstraction and Encapsulation
• The wrapping of data and functions into a
single unit (called class) known as
encapsulation.

• Abstraction refers to the act of representing


essential features without including the
background details or explanations.

Tuesday, February 5, 12
2019
Inheritance
• Inheritance is the process by which objects
of one class acquire the properties of objects
of another class.
Vehicles

2 wheeler 4 wheeler

Scooter Bike Car Bus

Tuesday, February 5, 13
2019
Example for inheritence
• #include<iostream.h>
• #include<conio.h>
• class a
• {
• protected:
• int x;
• public:
• void disp()
• {
• cin>>x;
• cout<<"x is:"<<x<<endl;
• }
• };
• class b:public a
• {
• protected:
• int y;
• public:
• void disp()
• {
• cin>>y;
• cout<<"y is:"<<y<<endl;
• }
• };

Tuesday, February 5, 14
2019
• main()
• {
• b c1;
• clrscr();
• c1.a::disp();
• c1.b::disp();
• getch();
• }

Tuesday, February 5, 15
2019
Polymorphism
• Polymorphism means the ability to take
more than one form.
Shape

circle box triangle

Tuesday, February 5, 16
2019
Dynamic binding
• Binding refers to the linking of a procedure
call to the code to be executed in response
to the call.
• DB means that the code associated with a
given procedure call is not known until the
time of the call at run time.
• It is associated with polymorphism and
inheritance.

Tuesday, February 5, 17
2019
Message passing
• An object priented program consissts of a
set of objects that communicate with each
other.
• The following steps involved in it:
• Creating classes that define objects and
their behaviour,
• Creating objects from class definitions,
• Establishing communication among objects

Tuesday, February 5, 18
2019
comments
• C++ introduces a new comment symbol
//(double slash)
• The c comment symbols /*……*/ are valid
and are more suitable for multiline.

Tuesday, February 5, 19
2019
Output operator
• Cout << “ this is c++ than c”;
• The operator << is called the insertion or
put to operator.
• The iostream file
• #include <iostream> This header file
iostream should be included at the
beginning of all programs that use i/o
statements.

Tuesday, February 5, 20
2019
Input operators
• #include <iostream.h>
• Void main()
• {
• Float num1,num2,avg,sum;
• Cout <<“enter 2 nos”;
• Cin >>num1>>num2;
• Sum =num1+num2;
• Avg = sum/2;
• Cout<<“sum is :”<<sum;
• Cout<<“average is “<< avg;
• }
Tuesday, February 5, 21
2019
• #include<iostream.h>
• class person
• {
• char name[20];int age;
• public:
• void getdata();
• void disp();
• };
• void person::getdata()
• {
• cout<<" \n enter the name and age\n ";
• cin>>name>>age;
• }
• void person::disp()
• {
• cout<<" the name is :"<<name;
• cout<<"the age is :"<< age;
• }
• void main()
• {
• person a;
• a.getdata();
• a.disp();
• }
Tuesday, February 5, 22
2019
Inline function
An inline function is a function that expanded
in line when it is invoked .That is, the
compiler replaces the function call with the
corresponding function code.
Syntax :

Inline fn-header
{
Function body
}

Tuesday, February 5, 23
2019
Inline functions
#include <iostream.h>
inline float mult(float x,float y)
{ return(x*y); }
inline int div(int p,int q)
{ return (p+q); }
void main()
{ float a= 5.2, b= 5.5;
cout <<"\n\n\nthe result is\n\t ";
cout<<mult(a,b)<<"\n";
cout<<div(a,b);
}
Tuesday, February 5, 24
2019
Function overloading
• Overloading refers to the use of the same
thing for different purposes.
• C++ permits overloading of functions
means the same function name to create fns
that perform a variety of different tasks,this
is known as function polymorphism.

Tuesday, February 5, 25
2019
Function overloading
• #include <iostream.h>
• int volume(int);
• double volume(double, int);
• long volume (long,int,int);
• void main()
• {
• cout<<"the result is \n";
• cout<<volume(10)<<"\n";
• cout<<volume(2.5,8)<<"\n";
• cout<<volume(12l,7,12)<<"\n";
• }

Tuesday, February 5, 26
2019
• int volume(int s)
• {
• return(s*s);
• }
• double volume(double r,int h)
• {
• return(3.14*r*r*h);
• }
• long volume(long l,int b, int h)
• {
• return(l*b*h);
• }

Tuesday, February 5, 27
2019
Classes and objects
• A class is a way to bind the data and its
associated functions together. It allows the
data and functions to be hidden from
external use.
• Syntax
Class class_name
{
Private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
}

Tuesday, February 5, 28
2019
Class implementation
• #include<iostream.h>
• #include<conio.h>
• class emp
• {
• int no;
• char name[10];
• public:
• void get()
• {
• cout<< "\n\nenter no & name \n";
• cin>>no;
• cin >>name;
• }
• void print()
• {
• cout<<"name is "<< name;
• cout<<"\nno is " << no;
• }
• };
• void main()
• {
• emp e;
• clrscr();
• e.get();
• e.print();
• getch();
• }

Tuesday, February 5, 29
2019
Nesting of member function
• #include<iostream.h>
• #include<conio.h>
• class emp
• {
• int no;
• char name[10];
• public:
• void get()
• {
• cout<< "\n\nenter no & name \n";
• cin>>no;
• cin >>name;
• }
• void print()
• {
• get();
• cout<<"name is "<< name;
• cout<<"\nno is " << no;
• }
• };
• void main()
• {
• emp e;
• clrscr();
• e.print();
• getch();
• }
Tuesday, February 5, 30
2019
Private member functions

• A private member function can be called by


another public function that is a member of
its class.

Tuesday, February 5, 31
2019
• #include<iostream.h>
• #include<conio.h>
• class sample
• {
• private:
• void read()
• {
• cout<<"\n\n\naccessing private member function";
• cout<<" \nusing public member function we are calling the pvt fn";
• }
• public:
• void update();
• };
• void sample::update()
• {
• read();
• }
• void main()
• {
• sample a;
• a.update();
• getch();
• }

Tuesday, February 5, 32
2019
Static data members
• A data member of a class can be qualified as
static.
• Characteristics:
• It is initialized to zero when the first object
of its class is created. No other initialization
is permitted.
• Only one copy of that member is created for
the entire class and it is shared by all the
objects of that class.
• It is visible only within the class, but its
lifetime is the entire program.

Tuesday, February 5, 33
2019
Static member function

• A static fn can have access to only static


members declared in the same class.
• A static member fn can be called using the
class name instead of its objects
• E.g., class_name :: function _name;

Tuesday, February 5, 34
2019
Friend function

• A friend function is a non-member function


that is granted access to a class private
members.
• A member function operates upon the object
used to invoke it, while a friend function
operates on the object or objects passed to it
as arguments.

Tuesday, February 5, 35
2019
Friend function implementation
• #include<iostream.h>
• #include<conio.h>
• class one
• {
• private:
• char name[10];
• int no;
• public:
• void two();
• friend void print(one g);
• };
• void one::two()
• {
• cout<<"\nenter the no and name";
• cin>>no;
• cin>>name;
• cout<<no<<endl<<name;
• }

Tuesday, February 5, 36
2019
• void print(one a)
• { cout<<"\nfriend function\n";
• cout<<a.no<<endl;
• cout<<a.name;
• }
• void main()
• {
• clrscr();
• one a;
• a.two();
• print(a);
• getch();
• }
Tuesday, February 5, 37
2019
Constructors and destructors

• C++ provides a special member function


called the constructor which enables an
object to initialize itself when it is created.
• This is known as automatic initialization of
objects.
• It also provides another member function
called the destructor that destroys the
objects when they are no longer required.

Tuesday, February 5, 38
2019
• When a class contains a constructor it is sure that
an object created by the class will be initialized
automatically.
• A constructor that accepts no parametrs is called
the default constructor.
• Characteristics:
• They s’d be declared in the public section.
• They are invoked automatically when the objects
are created.
• They don not have return types, though a derived
class can call the base class constructor.
• We cannot refer to their address
• When a constructor is declared for a class
,initialization of the class objects becomes
mandatory.

Tuesday, February 5, 39
2019
• #include<iostream.h>
• #include<conio.h>
• class emp
• {
• int no;
• char name[10];
• public:
• emp()
• {
• cout<<"\nthis is constructor";
• cout<<"\n ++++++++++++++++++++";
• }
• ~emp()
• {
• cout<<"\nthis is destructor";
• cout<<"\n ++++++++++++++++++++";
• }

Tuesday, February 5, 40
2019
• void get()
• {
• cout<< "\n\nenter no & name \n";
• cin>>no;
• cin >>name;
• }
• void print()
• {
• cout<<"name is "<< name;
• cout<<"\nno is " << no;
• }
• };
• void main()
• {
• clrscr();
• emp e;
• e.get();
• e.print();
• getch();
• }

Tuesday, February 5, 41
2019
output

• this is constructor
• ++++++++++++++++++++

• enter no & name


• 678
• example
• name is example
• no is 678 - alt+f5
• this is destructor
• ++++++++++++++++++++

Tuesday, February 5, 42
2019
Parameterized constructors
• The constructors that can take arguments
are called parameterized construcotrs.
• E.g
• Class stud
• {
int n,m;
Public:
Stud (int x, int y)

Tuesday, February 5, 43
2019
Copy constructor
• C++ provides a mechanism for changing
how object initialization works.it consist of
defining a special constructor, called a copy
constructor.
• Syntax
• X::x(const x&)

Tuesday, February 5, 44
2019
• #include <iostream.h>
• Class alpha
• {
• Public:
• Alpha()
• {
• }
• Alpha(int a)
• { data = d;
• }
• Alpha(alpha& a)
• {
• Data = a.data;
• Cout<<“\n copy constructor invoked”;
• }

Tuesday, February 5, 45
2019
• Void display()
• {
• Cout<<data<<endl;
• }
• Private:
• Int data;
• };
• Void main()
• {
• Alpha a1(50);
• Alpha a2(a1); or a2=a1;
• Cout<<“\n a3 = “;
• A2.display();
• }
• Output:
• copy constructor invoked
• a3 = 50

Tuesday, February 5, 46
2019
Operator overloading
• It is the process of assigning two or more operations to the same
operator.
• Depending on how the operator is used the c++compiler will determine
the actual operation to be performed.

Tuesday, February 5, 47
2019
Inheritance
• Classes can be reused in several ways.
• Once a class has been written and tested,it can be
adapted by other programmers to suit their
requirements.
• This is basically done by creating new classes,
reusing the properties of the existing ones.
• The mechanism of deriving a new class from an
old one is called inheritance.
• The old class is referred as base class and the new
one is called subclass or derived class

Tuesday, February 5, 48
2019
Types
1) Single inheritance

A base class

B derived class

Tuesday, February 5, 49
2019
2) Multiple inheritance

A B

Tuesday, February 5, 50
2019
3) Hierarchical inheritance
A

b c d

Tuesday, February 5, 51
2019
4) Multilevel inheritance
5) Hybrid inheritance

A A

B
B C

Tuesday, February 5, 52
2019
Example for single inheritance
• #include<iostream.h>
• #include<conio.h>
• class emp
• {
• int no;
• char name[10];
• public:
• void get()
• {
• cout<< "\n\nenter no & name \n";
• cin>>no;
• cin >>name;
• }
• void print()
• {
• cout<<"\n\nname is "<< name;
• cout<<"\nno is " << no;
• }
• };
Tuesday, February 5, 53
2019
• class add:public emp
• {
• char add[50];
• public:
• void disp()
• {
• print();
• cout<<"\nenter the location\n";
• cin>>add;
• cout<<"\n the location is : "<<add;
• }
• };
• void main()
• {
• add z;
• clrscr();
• z.get();
• z.disp();
• getch();
• }
Tuesday, February 5, 54
2019
Example for Multiple inheritance
• #include<iostream.h>
• class stud
• {
• private:
• int no;
• char code[3];
• public:
• void one()
• {
• cout<<"\nenter code";
• cin>>no;
• cout <<" \n\nthe code is :"<<no;
• }
• };

Tuesday, February 5, 55
2019
• class detail
• {
• char bg[2];
• public:
• void two()
• {
• cout<<"\n\nexample for multiple inheritance";
• cout<<"\nenter the blood group";
• cin>>bg;
• }
• void print()
• {
• cout<< "the blood group is "<<bg;
• }
• };
Tuesday, February 5, 56
2019
• class stud_detail:public stud,public detail
• {
• int tno;
• public:
• void last()
• {
• cout<<"\nenter tno";
• cin>>tno;
• cout<<"\nthe token no is :"<<tno;
• }
• };
• void main()
• {
• stud_detail s;
• s.one();
• s.two();
• s.last();
• s.print();
• }

Tuesday, February 5, 57
2019
Example for Multilevel inheritance
• #include<iostream.h>
• class stud
• {
• private:
• int no;
• char code[3];
• public:
• void one()
• {
• cout<<"\nenter code";
• cin>>no;
• cout <<" \n\nyou have entered the code is :"<<no;
• }
• };

Tuesday, February 5, 58
2019
• class detail:public stud //first level inheritance
• {
• char bg[2];
• public:
• void two()
• {
• cout<<"\n\nexample for multi_level inheritance";
• cout<<"\n\nenter the blood group :" ;
• cin>>bg;
• }
• void print()
• {
• cout<< "\nthe blood group is : "<<bg;
• }
• };

Tuesday, February 5, 59
2019
• class stud_detail:public detail //second level inheritance
• {
• int tno;
• public:
• void last()
• {
• cout<<"\nenter tno : ";
• cin>>tno;
• cout<<"\nthe token no is :"<<tno;
• }
• };
• void main()
• {
• stud_detail s;
• s.one();
• s.two();
• s.last();
• s.print();
• }

Tuesday, February 5, 60
2019
Example for hybrid inheritance
• #include<iostream.h>
• class student
• {
• int roll_no;
• public:
• void get_no()
• {
• cout<<"\nenter the roll number : ";
• cin>>roll_no;
• }
• void put_number()
• {
• cout<<" \nthe roll numbner is :"<<roll_no;
• }
• };
Tuesday, February 5, 61
2019
• class test:public student
• {
• protected:
• float mark1,mark2;
• public:
• void get_mark()
• {
• cout <<" \nenter the marks ";
• cout<<"\n enter mark1 : ";
• cin>>mark1;
• cout<<"\nenter mark2 :";
• cin>>mark2;
• }
• void put_mark()
• {
• cout<<" \nthe mark1 is :"<<mark1;
• cout<<" \nthe mark2 is :"<<mark2;
• }
• };

Tuesday, February 5, 62
2019
• class sports
• {
• protected:
• float score;
• public:
• void get_score()
• {
• cout<<"\nenter the score";
• cin>>score;
• }
• void put_score()
• {
• cout<<"\nthe score is : "<<score;
• }
• };

Tuesday, February 5, 63
2019
• class result:public test,public sports
• {
• private:
• float total;
• public:
• void display()
• {
• total = mark1+mark2+score;
• cout<<"\n total is : "<<total<<endl;
• }
• };
• void main()
• {
• result s1;
• s1.get_no();
• s1.get_mark();
• s1.get_score();
• s1.put_number();
• s1.put_mark();
• s1.put_score();
• s1.display();
• }

Tuesday, February 5, 64
2019
Polymorphism -Function overloading
• Polymorphism refers to ‘one name having many forms’.
• We can say ‘the same function name performs a variety of
different tasks.’
• This is known as function polymorphism.
• Using this concept we can design a family of fn with one
name but with different argument lists.
• The fn w’d perform diff’t operations depending on the
argument list in the fn call
• The correct fn to be invoked is determined by checking the
number &type of the arguments

Tuesday, February 5, 65
2019
Example program for function overloading
• #include<iostream.h>
• #include<conio.h>
• void area(int);
• void area(int,int);
• void main()
• {
• clrscr();
• area(10);
• area(10,20);
• getch();
• }
• void area(int x)
• { cout<<"function overloading program";
• cout<<"\narea of a square "<<x*x;
• }
• void area(int x,int y)
• { cout<<"\n area of a rectangle "<<2*x*y;
• }

Tuesday, February 5, 66
2019
Operator overloading

• It is the process of assigning two or more


operations to the same operator.
• The process involves the following steps:
• 1) create a class that defines the data type that is to
be used in the overloading operation
• 2) declare the operator fn operator op() in the
public part of the class. It may be either a member
fn or a friend function
• 3) define the operator fn to implement the required
operations.
• 4) keyword operator followed by the symbol

Tuesday, February 5, 67
2019
Example for operatorover loading
• #include<iostream.h>
• #include<conio.h>
• #include<iomanip.h>

• class over
• {
• public:
• int a,b;
• void get(int x,int y)
• {
• a=x;
• b=y;
• }
Tuesday, February 5, 68
2019
• void operator++()
• {
• a++;
• b++;
• cout<<endl<<"Incremented value"<<endl;
• cout<<a<<setw(5)<<b;
• }
• void operator-()
• {
• a=-a;
• b=-b;
• cout<<endl<<"Inverse is:"<<endl;
• cout<<a<<b;
• }
• over operator *(over ob2)
• {
• over t;
• t.a=a*ob2.a;
• t.b=b*ob2.b;
• return (t);
• }
• };

Tuesday, February 5, 69
2019
• void main()
• {
• clrscr();
• int x,y;
• cout<<endl<<"Enter the values of a,b"<<endl;
• cin>>x>>y;
• over ob1,ob2,ob3;
• ob1.get(x,y);
• ++ob1;
• ob2.get(x,y);
• -ob2;
• ob3=ob1*ob2;
• cout<<endl<<"The multiplied
value"<<endl<<ob3.a<<endl;
• getch();
• }

Tuesday, February 5, 70
2019
Tuesday, February 5, 71
2019
templates
• Class templates
• Function templates
• Member function templates
• Templates arguments
• Advantages of templates

Tuesday, February 5, 72
2019
Templates - definition

• Templates enable you to define generic


classes and generic funtions.
• A template can be used to create a family of
classes or functions.
• E.g a class template for an array enable to
create arrays of various data types such as
int array and float array etc.,

Tuesday, February 5, 73
2019

Potrebbero piacerti anche