programming is nothing but problem solving. The different ways (problem solving
techniques)are available.
among all object oriented programming is considered as best technique to solve t
he particular problem.
object oriented programming is useful to solve realworld problems(means requirem
ent to develop software to manage business)
procedure oriented vs object oriented programming
============================================
Procedure Oriented Programming
cout:
---------cout is a predefined object that represents the standard output stream in c++.
here the standard output stream represents the screen.
the operator << is called the insertion or "put to" operator. it inserts or send
s the
contents of the variable on its right to the object on its left
cin:
------it is an input statement and causes the program to wait for the user to type the
input
it is a predefined object in c++ that corresponds to the standard input stream,
here input stream represents the keyboard.
the operator>> is know as extraction or " get from " operator.
it extracts or takes the value from the keyboard and assigns it to the variable
on
its right
Objects:
--------------any realworld entity(thing) can be considered as object .
object will have properties and behaviour
properties means data(varibles which has values) and behaviour means functions w
hich
operates on objects data
ex :
consider bank account as an object
bank account will have the following properties
char accholdername[15];
int balance
int accno..........etc
bank account will have the following behaviour
void deposit();
void withdraw();
objects contains data and functions
class variables are called objects
without class there is no object.
instance of a class is called as an object.
Objects are identified by its unique name.
There can be more than one instance of an object.
Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member
functions also known as methods.
Classes:
---------------Classes are user defined data types based on which objects are
created.
class is a blue print or a plan or a sketch based on which objects are
created.
if class is a plan then building is an object.
without plan there is no building.
by using single plan we can create multiple number of buildings
of same type.
Objects with similar properties and methods are grouped together
to form a Class.
Thus a Class represent a set of individual objects.
Characteristics of an object are represented in a class as Properties.
The actions that can be performed by objects becomes functions
of the class
and is referred to as Methods.
Example #1:
For example consider we have a Class of Cars under which Santro Xing, Alto and W
aganR represents individual Objects.In this context each Car Object will have it
s own, Model,Year of Manufacture, Colour, Top Speed, Engine Power etc.,which for
m Properties of the Car class and the associated actions i.e., object functions
like Start, Move, Stop form the Methods of Car Class.No memory is allocated when
a class is created. Memory is
For Eg.,
class Address
{
String name;
Srting H.no;
String Street name;
}
class LatestAddress extends Address
{
String City;
String State;
String Country;
}
public class Person
{
{
LatestAddress la = new LatestAddress();
//Assign variable accordingly...
}
}
In the above Example class LatestAddress getting all features from the Address c
lass.
In the LatestAddress class we have total 6 properties..3 are inherited from Addr
ess class and 3 properties are
incorporated. So In the class Person we are declaring the object of class Latest
Address and then assign new variables using the properties of the previous base
classes... So this is a nice example of inheritance..
Polymorphism :
-----------------------Polymorphism means ability to take more than one form that
an operation can exhibit different behavior at different instance
depend upon the data passed in the operation.
1>We behave differently in front of elders, and friends. A single person is beha
ving differently at different time.
2> A software engineer can perform different task at different instance of time
depending on the task assigned to him .He can done coding , testing , analysis
and designing depending on the task assign and the requirement.
3> Consider the stadium of common wealth games. Single stadium but it perform mu
ltiple task like swimming, lawn tennis etc.
4> If a girl is married and mother of 2 children doing teaching job then she is
a women first ,, teacher in a school when she is in school,,wife of someone at
home,, mother of her children,, and obvious daughter of someone & may be girl fr
iend of someone (just kidding) means a woman plays different roles at different
times that's polymorphism (many forms).
Summary:
OOPs have following features:
1.
2.
3.
4.
5.
Object
- Instance of Class
Class
- Blue print of Object
Encapsulation
- Protecting our Data
Polymorphism - Different behaviors at different instances
Abstraction
- Hiding our irrelevant Data
6. Inheritance
Data Hiding :----------------------1. It s the process of keeping the Data under such an Access mode
that its only accessible to permitted Functions.
2. Using Private, Protected specifiers, we can hide Data from
access from outside a Class.
3. Functions, Structures and Classes implement Data Hiding
at programmer level.
Dynamic binding :---------------------------1. Its also called Runtime Polymorphism, where the compiler
will do the binding
between the function call and with its corresponding function
definition (code) during runtime.
2. Its implemented using Virtual Functions, during Inheritance.
Message Passing :--------------------------------1. It s the process of passing Data between Objects of same
Class and also of different Classes.
2. Its implemented using Function calling or by simply sharing data.
REFERENCE VARIABLES :
A reference variable provides an alias (Alternative name) for a
previously defined variable.
A reference variable must be initialized at the time of
declaration.
Datatype &ref_var = variable;
Access Control in Classes:(access specifiers)
----------------------------------------Access specifiers in C++ class defines the access control rules.
C++ has 3 new keywords introduced, namely,
access specifiers are used to give the access permissions to
the members of the class(i.e. data members and member functions)
public
private
protected
Public:
-------------Public, means all the class members declared under public will be
available to everyone. The data members and member functions
declared public can be accessed by other classes too.
Hence there are chances that they might change them.
So the key members must not be declared public.
class Abc
{
public: // public access specifier
int x;
// Data Member Declaration
void display(); // Member Function decaration
}
Private:
---------Private keyword, means that no one can access the class members
declared private outside that class.
If someone tries to access the private member, they will get a compile
time error. By default class variables and member
functions are private.
class Abc
{
private: // private access specifier
int x;
// Data Member Declaration
void display(); // Member Function decaration
}
Protected
-----------------Protected, is the last access specifier, and it
it makes class member inaccessible outside the
can be accessed by any subclass of that class.
(If class A is inherited by class B, then class
in calss B we can access the protected members
is similar to private,
class. But they
B is subclass of class A.
of A.)
class Abc
{
protected: // protected access specifier
int x;
// Data Member Declaration
void display(); // Member Function decaration
}
{
public:
int rollno;
string name;
};
Here A and B are the objects of class Student, declared with the class definitio
n.
We can also declare objects separately, like we declare variable of primitive d
ata
types. In this case the data type is the class name, and variable is the object.
void main()
{
Student A,B;
}
Both A and B will have their own copies of data members.
Accessing data Members of Class
------------------------------------------------Accessing a data member depends on the access control(access specifiers)
of that data member of the class.
If its public, then the data member can be easily accessed using the
direct member access (.) operator with the object of that class.
syntax:
classobject. datamemberof the class;
Ex: A.x;
Accessing Public Data Members
----------------------------------------------------Following is an example to show you how to initialize and use the
public data members using the dot (.) operator and the respective object of clas
s.
class Student
{
public:
int rollno;
string name;
};
int main()
{
Student A;
Student B;
A.rollno=1;
A.name="Adam";
B.rollno=2;
B.name="Bella";
cout <<"Name and Roll no of A is :"<< A.name << A.rollno;
cout <<"Name and Roll no of B is :"<< B.name << B.rollno;
}
If, the
then we
Then we
use or
to use the \
scope resolution :: operator along with class name alng with
function name.
Example :
class Cube
{
public:
int side;
int getVolume();
n type int.
};
example2:
But if we plan to define the member function outside
the class definition then we must declare the function inside class definition a
nd then define it outside.
class Cube
{
public:
int side;
int getVolume();
}
int Cube :: getVolume()
// defined outside class definition
{
return side*side*side;
}
int main()
{
Cube C1;
C1.side=4; // setting side value
cout<< "Volume of cube C1 ="<< C1.getVolume();
}
Static Members :----------------------------Normally each object of a class have separate copies of all data
members.
But when a variable is declared static, only one copy of the variable
will be stored and all the objects of the class will share the variable.
When any statement is made to the static variable of any object,
the change is effective for all the objects of the class.
1. Only a single copy of the Static members are shared between
Objects of the Class.
2. We can create Static Data Members and Static Member Functions.
3. Static Member Functions can have direct access to Static Members
of that Class.
4. Static Members are called using the Class-Name and
Scope-Resolution-Operator and Member-Name
syntax:
classname::mebername;
5. The Static members are declared using static
keyword.
6. Syntax :For functions :- static return-type function-name ( input arguments )
{ function
definition codes }
For variables :- static data-type variable-name ;
function overloading:
--------------------------------1) It s a form of Polymorphism.
2) Here we can create more than one definition of function
with same Name. But the number of input arguments
should be different for all the definitions or data-type of
input arguments should be different.
3) According to the type or number of input arguments given
during calling of the function, the compiler binds the specific
definition with specific call of function.
ex:func_ove.cpp
Inline Functions :-------------------------------Are those functions whose call is replaced by their body during
compilation.
1. By default every function created in C++ is Inline.
2. These functions are expanded in line,
The function definition code is placed in place of the calling of the
function during compile time by the compiler.
3. To make any Function Inline we can explicitly mention the keyword
inline
before function declaration.
4. Syntax :- inline return-type function-name ( input argument list )
{
coding for
function definition
}
6. Situations where any Inline Function does not remain Inline
although it is declared Inline.
1) When a return statement is used.
2) When loops, switch or goto statements are used.
3) When static variables are used.
4) When recursion is used.
example: inline.cpp
Operator Overloading :-----------------------------------C++ has the ability to provide the operator with a special meaning
for a data type.
The mechanism of giving such special meanings to an operator is
known as operator
overloading.
To define an additional task to an operator.
This is done with help of a special function, called operator function,
which describes the task.
The general form of an operator function is:
return type operator op(argument list)
{
function_body();
}
Where return type is the type of value returned by the specified
operation and op is the operator being overloaded.
The keyword "operator" precedes the "op". Operator op
is the function name.
1. It s a type of Polymorphism, where we can give additional meaning
or
implementation to existing operators.
2. We define the overloaded operator as a function with
operator keyword
attached before the operator-symbol.
Overloaded operator is used to perform operation on
user-defined data type.
For example '+' operator can be overloaded to perform addition on
various data types, like for Integer, String(concatenation) etc.
Almost any operator can be overloaded in C++. However there are
few operator which can not be overloaded.
Operator that are not overloaded are follows
scope operator ::
sizeof
member selector .
member pointer selector *
ternary operator ?:
Inheritance :----------------------1. It s a method of implementing reusability of Classes.
2. The Members of one Class can be accumulated
in another Class through Inheritance.
3. The Class which acts as the source of providing its Members
for Inheritance is called the Parent or Base Class.
The Class that derives from or acquires the Members of Base
Class is called the Child or Derived Class.
4. The Private members of any Class can Not be Inherited.
5. The process of Inheritance can be categorized in to
Three main Categories :1) Public Inheritance :- Here the Child Class acquires the Protected and Public
Members. And they Remain Protected and Public in the Child Class. Its done by
using public
keyword during Child Class definition.
Syntax :class child-class-name : public parent-class-name
{ definition of the child class
} ;
2) Private Inheritance :- Here the Child Class acquires the Protected and Public
Members. And they Become Private in the Child Class. Its done by using private
keyword during Child Class definition.
Syntax :-
Example :- multiple.cpp
Hierarchical Inheritance :-----------------------------------------1) Here two or more Child classes Inherit the same Parent class.
2) Syntax :class child1-class-name : <access> parent1-class-name
{
child1 class definition
} ;
class child2-class-name : <access> parent1-class-name
{
child2 class definition
} ;
Example :-Hierarchical.cpp
Hybrid Inheritance :----------------------------------1) It s a combination of single-level, multi-level, multiple and hierarchical Inhe
ritance.
2) Here the resultant (last or grand-child) Child class in the structure
can acquire duplicate copies from the grand parent class.
3) In situations where the resultant child class can acquire duplicate
copies of the grand parent class then grand parent class used during
inheritance is accompanied with
virtual keyword, to avoid
duplicacy in the grand child class.
Syntax :class grand-parent1-class-name
{
definition of grand-parent-class
}
class parent1-class-name : virtual <access> grand-parent1-class-name
{
definition code for parent1-class
} ;
class parent2-class-name : virtual <access> grand-parent1-class-name
{
definition code for parent2-class
} ;
class grand-child-class-name : <access> parent1-class-name , <access> parent2class-name { definition code for grand-child-class } ;
Example :- Hybrid.cpp
Pointer to Derived Classes :----------------------------------------1. They are declared as pointers along with the class name as the
data-type.
2. To access the members of the class arrow symbol -> is used.
3. Its observed that the pointer of Parent class can point towards
the objects of its Child class.
But a Child class pointer cannot point towards Parent class.
Class A";
Class B";
Virtual Functions :--------------------------1. Virtual functions are defined using the keyword virtual
before function declaration.
2. The access specifiers effect the accessibility of Virtual Functions.
3. Usually the Parent class pointer cannot access the member
functions of its Child
classes. Only when the prototype is same in both the Parent as well
as Child classes
and the function is declared as Virtual in the Parent class then
it can be done.
4. If we desire to call the Child class function using the pointer
of Parent class then :1) In both the classes we have to define the function with same
prototype.
2) In the Parent class we need to use virtual keyword
during defining the function.
3) Now when we assign the address of Child class object to the pointer
of the Parent
class and using arrow operator we can call the function
of the Child class.
class A
{
public:
virtual void show() //The show() function is declared Virtual
{
cout<<"\nWe are in Class A";
}
};
class B:public A
{
public:
void show()
{
cout<<"\nWe are in Class B";
}
};
main()
{
clrscr();
A oba;
B obb;
A *pa;
pa=&oba;
pa->show(); //This calls Class A show()
pa=&obb;
pa->show(); //This calls Class B show()
getch();
}
Dynamic Binding or Runtime Polymorphism :------------------------------------------------------------------1. It s the process of deciding at Runtime to either
call the Parent class function
or Child class function using Parent class Pointer.
2. Example :#include<iostream.h>
#include<conio.h>
#include<process.h>
class A
{
public:
virtual void show()
{
cout<<"\nWe are in Class A";
}
};
class B:public A
{
public:
void show()
{
cout<<"\nWe are in Class B";
}
};
main()
{
clrscr();
A *pa;
int inp;
while(1)
{
cout<<"\nEnter 1 for Invoking Class A show()";
cout<<"\nEnter 2 for Invoking Class B show()";
cout<<"\nEnter 0 to EXIT\n\n";
cin>>inp;
switch(inp)
{
case 1: pa=new A;
//creating object of parent class A at Runtime and
//assigning its address to pointer of parent class A
pa->show(); //Invoking Class A's show()
break;
case 2: pa=new B;
//creating object of child class B at Runtime and
//assigning its address to pointer of parent class A
pa->show(); //Invoking Class B's show()
break;
case 0: exit(0);
}
}
getch();
}
Pure Virtual Functions and Abstract Classes :--------------------------------------------------------------------1. These are the functions that are declared as Virtual at the same
time assigned to Zero value.
2. They don t have any definition, they are only declared and
equated to Zero.
3. If a Class contains one or more Pure Virtual functions then
that Class becomes
Abstract.
4. Objects of Abstract classes can Not be created.
5. If we Inherit an Abstract class then we have to Redefine
all the Parent
class Pure Virtual functions in the Child class,
otherwise the Child class would also become an Abstract
class.
6. Syntax :- virtual function-name ( parameter list ,if any ) = 0 ;
7. Example :class A //It has become Abstract so No objects can be created from it
{
virtual void show()=0; //Pure Virtual Function
};
class B:public A
{
public:
//Here we need to redefine show() otherwise class B also would
// become Abstract
void show()
{
cout<<"\nWe are in Class B";
}
};
void main()
{
clrscr();
// A ob1; //Error as A is Abstract, so class A objects can not be created
B ob2;
ob2.show();
getch();
}
I/O Streams :-------------------------1. A Stream is a queue of memory spaces or buffers that are used to
connect I/O Devices and Disk Files with our programs.
2. We use Input Streams to connect our program with Input Devices
or to Receive data from Disk Files.
3. We use Output Streams to connect our program with Output Devices
or to Write data in to Disk Files.
4. All the I/O Streams are in the form of inbuilt Classes.
5. The Stream Class use for receiving input from any Input Device
ISTREAM and from Disk File is IFSTREAM
(which is derived form ISTREAM class).
6. The Stream Class use for sending output to any Output Device
OSTREAM and to Disk File is OFSTREAM
(which is derived form OSTREAM class).
7. All the Input and Output Stream Classes are Inherited from
the grand parent IOS Class.
8. The IOSTREAM class for I/O Devices is derived from multiple
inheritance from ISTREAM and OSTREAM classes.
9. The FSTREAM class for Disk Files is derived directly
from IOSTREAM class.
Inbuilt Formatted Console Output functions :----------------------------------------------------------------1. The inbuilt console Output operations can be used to set Format
of how the output would appear.
2. All the console I/O functions are available in IOSTREAM class.
Using cin object we can access the Input functions
and using cout object we can access the Output functions.
3. Functions and their Use :Functions
Use
------------------------------------------------------------cout.setf(ios::left ) ;
For displaying the matter using cout, as Left Ju
stified
cout.setf(ios::right ) ;
For displaying the matter using cout as Right Ju
stified
cout.setf(ios::scientific) ;
For displaying the matter using cout in exponent
ial (e ) format.
cout.setf(ios::oct ) ;
For displaying the matter in Octal format.
cout.setf(ios::hex ) ;
For displaying the matter in Hexadecimal format.
cout . fill ( char ) ;
For displaying the specified characters instead
of blank spaces.
cout . width ( int ) ;
Setting the number of columns in which the conte
nt
has to be displyed. If the content length is less than
the specified columns then the content will be justified
according to the setf() function. If the columns
specified is less than the matter length then the width
is ignored and the full content is displayed. By default
its left justified.
cout . precision ( int ) ;
It specifies the number of digits to be displaye
d after
ios :
getch();
}
Templates :1. There are two main categories of Templates :1) Function Templates and 2) Class Templates.
2. They are used for a Generic representation of Data-types ,
where multiple data-types can be implemented using the same
definition of template.
3. It s a kind of Data-Type Polymorphism.
4. The Data-types need to be decided at compile time.
5. Syntax for Function Templates :template <class T1, class T2, ., class Tn>
return-type function-name(T1 a, ., Tn z)
{ function definition }
Here we need to use the templates to be used in the input argument
list
6. Syntax for Class Templates :template <class T1, class T2, ., class Tn>
class class-name
{
class definition
}
Here we need to use the templates to be used for variable
declarations in the class.
During creating Objects we need to specify the Data-Types
along with the class name
class-name<data-type1, data-type2, , data-type n> object ;
7. Example :Exception Handling :1. It s a procedure to ensure that during runtime if a specific
error is encountered
then this facility would help to handle the condition and necessary
actions can be taken, rather than the system terminating our program
abruptly.
2. For exception handling we use
try-throw-catch
statement.
3. Using try-catch block either a specific exception can be handled
or all exceptions can be handled.
4. The program code that can possibly generate exception
is put inside
try
block.
When an exception condition occurs then throw
clause
is used to throw an exception. The
catch
block is used to catch the thrown exceptions.
5. Syntax for handling specific exceptions :try
{
// Exception condition occurred so we use throw clause.
throw value1;