C Presentation
C Presentation
Eg. Ada
Object Oriented Programming
Inheritance & Polymorphism are included
eg. Simula,Smalltalk80
Class
Classes are called as the building blocks in the OOPs programs
Class are equivalent to the user defined functions in C.
A class contains variables which are called as Attributes and
Functions which are called as Methods
The default access of the class members is private.
Classes are standalone components and can be distributed
individually or as part of a library.
In short , a class is a blueprint for an object
Object
An entity that can store data and send and receive
message. An instance of a class.
A class is used to create an object
Each Object has its own attributes ( fields) and behavior
(Methods) are shared .
The data stored within an object represents the state of
the object. This data is called Attributes.
The Behavior of an object is what the object can do.This
is called Methods
A class must be defined before creating an object
Example
public class student
{
private: // Access Modifier
char name[10]; // attributes
int rollno;
public :
void getData(); // methods
void dispData();
};
void main()
{
student s1; // Declaring an object
s1.getData(); // Calling public method of a class
s1.dispData();
}
Example:
void student :: getData ()
{
printf(“Enter Student Name: “);
gets(name);
printf(“Enter Student RollNo:”);
scanf(“%d”, &rollno);
}
void student :: dispData ()
{
printf(“/nStudent Name : %s “, name);
printf(“/nStudent RollNo : %d”, rollno);
}
Abstraction:
The act of representing the essential features of something
without including much background details or explanations
Data Abstraction:
Classes use the concept of abstraction and are defined as a list
of abstract attributes such as size,weight and cost , and
functions to operate on these attributes. They encapsulate all
the essential properties of the objects that are to be created.
Since the classes use the concept of Data Abstraction,they are
known as Abstract Data Types(ADT)
Data Encapsulation:
The wrapping up of the data and functions into a single
unit called class is known as encapsulation.
Data cannot be accessed from outside directly without
function.
The functions provide interface between the object’s data
and program.
By default all class members are having private access
modifier. One can specify protected or public access
modifier to access the data .
In this way Data Hiding (Information Hiding) can be
achieved.
Polymorphism:
Polymorphism means the ability to take
more than one forms.
Eg:
12 + 15 = 27
Poly + morphism = Polymorphism.
Thus polymorphism plays an important role in
allowing objects having different internal
structures to share the same external interface
Types of Polymorphism
Polymorphism
RUNTIME POLYMORPHISM:
Also called Late Binding or Dynamic Binding
e.g: Virtual Functions
Overloading :
A language feature that allows a function or operator to be given
more than one definition
Function Overloading:
Same Name different argument list. It can be in the same class or in
the derived class.Return type can or cannot be same.
Which function will be called is decided at compile time by seeing
the types of arguments, as it is called as Compile time polymorphism
Eg: void display(int x) // display() is overloaded
int display(int x,int y)
Operator Overloading:
Operator overloading allows you to change the meaning of an
operator.
For e.g. most people, when they see a plus sign(+), assume it
represents addition of numeric data. However,there are times ,
when a plus sign represent something else. In the context of
strings, the plus sign does not mean addition of integers or floats,
but Concatenation of strings. It means C++ tries to make the user
defined data type behave in much of the same way as the built in
types. The mechanism of giving such special meanings to an
operator is called Operator overloading.
e.g char *s1=“hello”; char *s2=“ world”;
s1+s2
Operator Overloading:
void display()
{
printf(“ Display Base”);
}
}
};
void show()
{
printf(“ show Base”);
}
};
void main()
{
Base B;
Derived D
Base *bptr;
circle triangle
Eg :
square
TYPES OF INHERITANCE
SINGLE INHERITANCE
Bderived from A
MULTIPLE INHERITANCE
Cderived from A & B
HIERARCHICAL INHERITANCE
B, C,D derived from A
MULTILEVEL INHERITANCE
B derived from A and C derived from B
HYBRID INHERITANCE
B & C derived from A & D derived from
B&C
Members variables and their acess:
Same Is Is visible Is
class visible visible
Derived Not Is visible Is
Class visible visible
Outside Not Not visible Is
Class visible visible
Access of members
a. Aggregation
b. Association
Aggregation:
Aggregation means that a complex object is composed of other objects
where you normally see the object as a whole and are unable to see the
internal other objects.
Eg Television , car etc.
Association:
In association both the whole object as well as the parts can be
represented.
Eg Computer,stereo system etc.
Composition and Inheritance
Composition always represents Has-a Relationship and Inheritance
always represents Is-a Relationship.
Eg: car has a steering------Composition
Eg:student is a sportsman.----Inheritance
Message Passing:
The phenomenon of one object communicating with other is
called Message Passing.
e.g. public class Payroll{
char name[10];
public:
void getData();
};
void main()
{ Payroll p;
p.getData();
}
void Payroll : getData ()
{ Person p;
p.setname(“Joe”); // Payroll object is sending message to Person object
}
Mutators :
Functions which changes or set the attributes are called Mutators
Insepectors :
Functions which determine the attributes of an objects are called
Inspectors
Facilitators :
Functions that direct an Object to perform some functions or options
are called facilitators.
Constructors and Destructors
Constructors : are special functions written in any class.A
constructor does not have any return type as well as its name is same
as that of the class name. It is implicitly called always when an object
of a class is created. We can initialize all the attributes of the class in
the constructor.
Constructors may or may not accept parameters.
If we don’t provide any constructor than a default constructor is
provided by the complier.But if we provide a parameterized
constructor than one default constructor should also be provided.
In case of inheritance the base class constructor is called first then the
derived class constructor is called.
Destructor:
Destructors is the special function similar to a constructor .
Destructor is also having the signature same as constructor the
only difference is it is preceded by a tild (~).
A destructor is implicitly called when the object moves out of
scope.
When memory is allocated dynamically then we have to call
destructor explicitly.
Incase of Inheritance the destructor of derived class is called
first and then the destructor of the base class.
public class employee{
private: // Access Modifier
char ename[10]; // attributes
int empno;
public :
employee(); // Constructor
~employee() ; // Destructor
void setData(); // Mutators
void getData(); // Inspectors
void dispData(); // Facilitators
};
void main()
{ employee e1;
e1.setData();
e1.getData();
}
employee : employee() // Constructor
{
printf(“Constructor is called”);
}
employee :: ~employee() // Destructor
{
printf(“\nDesstructor is called”);
}
void employee :: setData() // Mutators
{ static num=1;
empno = num + 1;
}
void employee :: getData() // Inspectors
{
printf(“Enter your name:”);
gets(ename );
}
void employee :: dispData() // Facilitators
{
printf(“\n Employee name: %s”, ename);
printf(“\n Employee name: %d”, empno);
}
Friend Functions:
The functions which can access the private data members of a
class are called as friend functions of that class. A friend function
can be declared using a keyword friend.
Friend functions do not get inherited, i.e. a friend of a base class
need not be the friend of the derived class until we explicitly
declare it as a friend of the derived class.