Oop 1
Oop 1
Object Oriented Programming follows bottom up approach in program design and emphasizes on
safety and security of data..
Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The
base class is also known as parent class or super class.
Derived class is also known as a child class or sub class. Inheritance helps
in reusability of code , thus reducing the overall size of the program
Data Abstraction:
It refers to the act of representing essential features without including the background
details .Example : For driving , only accelerator, clutch and brake controls need to be
learnt rather than working of engine and other details.
Data Encapsulation:
It means wrapping up data and associated functions into one single unit called class..
A class groups its members into three sections :public, private and protected, where
private and protected members remain hidden from outside world and thereby helps in
implementing data hiding.
Modularity :
The act of partitioning a complex program into simpler fragments called modules is
called as modularity.
It reduces the complexity to some degree and
It creates a number of well defined boundaries within the program .
Polymorphism:
Poly means many and morphs mean form, so polymorphism means one name multiple
forms.
It is the ability for a message or data to be processed in more than one form.
C++ implements Polymorhism through Function Overloading , Operator overloading and
Virtual functions .
. SHAPE
area()
A Class is a group of similar objects . Objects share two characteristics: They all have state and
behavior. For example : Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current
speed) and behavior (changing gear, applying brakes). Identifying the state and behavior for real-
world objects is a great way to begin thinking in terms of object-oriented programming. These
real-world observations all translate into the world of object-oriented programming.
Software objects are conceptually similar to real-world objects: they too consist of state and
related behavior. An object stores its state in fields (variables in some programming languages)
and exposes its behavior through functions
Classes in Programming :
Declaration/Definition :
A class definition starts with the keyword class followed by the class name; and the class body,
enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a
list of declarations.
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Where class_name is a valid identifier for the class, object_names is an optional list of names for
objects of this class. The body of the declaration can contain members that can either be data or
function declarations, and optionally access specifiers.
public:
17
Access specifiers in Classes:
Access specifiers are used to identify access rights for the data and member functions of the class.
There are three main types of access specifiers in C++ programming language:
private
public
protected
Member-Access Control
Type of Access Meaning
Access control helps prevent you from using objects in ways they were not intended to be
used. Thus it helps in implementing data hiding and data abstraction.
OBJECTS in C++:
Objects represent instances of a class. Objects are basic run time entities in an object oriented
system.
Class_name object_name;
In C++, a class variable is known as an object. The declaration of an object is similar to that of a
variable of any data type. The members of a class are accessed or referenced using object of a
class.
Both of the objects Box1 and Box2 will have their own copy of data members.
Object_name.Data_member=value;
The dot ('. ') used above is called the dot operator or class member access operator. The dot
operator is used to connect the object and the member function. The private data of a class can be
accessed only through the member function of that class.
The member functions of a class can be defined outside the class definitions. It is only declared
inside the class but defined outside the class. The general form of member function definition
outside the class definition is:
The scope resolution operator (::) specifies the class to which the member being declared
belongs, granting exactly the same scope properties as if this function definition was directly
included within the class definition
class sum
{
int A, B, Total;
public:
void getdata ();
void display ();
};
void sum:: getdata () // Function definition outside class definition Use of :: operator
{
cout<<” \n enter the value of A and B”;
cin>>A>>B;
}
void sum:: display () // Function definition outside class definition Use of :: operator
{
Total =A+B;
cout<<”\n the sum of A and B=”<<Total;
}
Inside the class definition
The member function of a class can be declared and defined inside the class definition.
class sum
{
int A, B, Total;
public:
void getdata ()
{
cout< ”\n enter the value of A and B”;
cin>>A>>B;
}
void display ()
{
total = A+B;
cout<<”\n the sum of A and B=”<<total;
}
};
Differences between struct and classes in C++
In C++, a structure is a class defined with the struct keyword.Its members and base classes
are public by default. A class defined with the class keyword has private members and base
classes by default. This is the only difference between structs and classes in C++.
INLINE FUNCTIONS
Example
{ cout<<a*a;}
void main()
{. In place of function
call , function body is
Square(4); { cout <<4*4;} substituted because
Square () is inline
Square(8) ; { cout <<8*8; } function
}
Pass Object As An Argument
/*C++ PROGRAM TO PASS OBJECT AS AN ARGUMEMT. The program Adds the two
heights given in feet and inches. */
#include< iostream.h>
#include< conio.h>
class height
{
int feet,inches;
public:
void getht(int f,int i)
{
feet=f;
inches=i;
}
void putheight()
{
cout< < "\nHeight is:"< < feet< < "feet\t"< < inches< < "inches"< < endl;
}
void sum(height a,height b)
{
height n;
n.feet = a.feet + b.feet;
n.inches = a.inches + b.inches;
if(n.inches ==12)
{
n.feet++;
n.inches = n.inches -12;
}
cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< < endl;
}};
void main()
{height h,d,a;
clrscr();
h.getht(6,5);
a.getht(2,7);
h.putheight();
a.putheight();
d.sum(h,a);
getch();
}
/**********OUTPUT***********
Height is:6feet 5inches
Height is:2feet 7inches
Height is 9 feet and 0
4 Marks Solved Problems :
Ans.
class TAXPAYER
{
char Name[30],PanNo[30];
float Taxabincm;
double TotTax;
void CompTax()
{ if(Taxabincm >500000)
TotTax= Taxabincm*0.15;
else if(Taxabincm>300000)
TotTax= Taxabincm*0.1;
Else if(Taxabincm>160000)
TotTax= Taxabincm*0.05;
else
TotTax=0.0; }
public:
TAXPAYER(char nm[], char pan[], float tax, double tax) //parameterized constructor
{ strcpy(Name,nm)
;
strcpy(PanNo,pan);
Taxabincm=tax;
TotTax=ttax; }
void INTAX()
{ gets(Name);
cin>>PanNo>>Taxabincm;
CompTax(); }
void OUTAX()
{ cout<<Name<<’\n’<<PanNo<<’\n’<<Taxabincm<<’\n’<<TotTax<<endl; }
};
Q 2 : Define a class HOTEL in C++ with the following description:
Private Members
Rno //Data Member to store Room No
Name //Data Member to store customer Name
Tariff //Data Member to store per day charge
NOD //Data Member to store Number of days
CALC //A function to calculate and return amount as
NOD*Tariff and if the value of NOD*Tariff is more than
10000 then as 1.05*NOD*Tariff
Public Members:
Checkin( ) //A function to enter the content RNo,Name, Tariff and
NOD
Checkout() //A function to display Rno, Name, Tariff, NOD
and Amount (Amount to be displayed by calling function CALC(
)
Solution :
#include<iostream.h>
class HOTEL
{ unsigned int Rno;
char Name[25];
unsigned int
Tariff; unsigned
int NOD; int
CALC()
{ int x;
x=NOD*Tariff;
if( x>10000)
return(1.05*NOD*Tariff);
else
return(NOD*Tariff);
}
public:
void Checkin()
{cin>>Rno>>Name>>Tariff>>NOD;}
void Checkout()
{cout<<Rno<<Name<<Tariff<<NOD<<CALC();}
};
Ans:class Applicant
{long ANo;
char Name[25];
float Agg;
char Grade;
void GradeMe( )
{ if (Agg > = 80)
Grade = ‘A’;
else if (Agg >= 65 && Agg < 80 )
Grade = ‘B’;
else if (Agg >= 50 && Agg < 65 )
Grade = ‘C;
else
} Grade = ‘D’;
public:
void Enter ( )
{ cout <<”\n Enter Admission No. “; cin>>ANo;
cout <<”\n Enter Name of the Applicant “; cin.getline(Name,25);
cout <<”\n Enter Aggregate Marks obtained by the Candidate :“; cin>>Agg;
GradeMe( );
}
void Result( )
{ cout <<”\n Admission No. “<<ANo;
cout <<”\n Name of the Applicant “;<<Name;
cout<<”\n Aggregate Marks obtained by the Candidate. “ << Agg;
cout<<\n Grade Obtained is “ << Grade ;
}
};
public members :
Readmarks() a function that reads marks and invoke the Calculate function.
Displaymarks() a function that prints the marks.