week 1 and 2
week 1 and 2
Week 2
Lecture 1 & 2
What is OOP?
Procedural programming is about writing procedures or functions that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
functions.
Page 1 of 12
Object Oriented Programming
done by making class members as private members of class. Private members can be
accessed only within the same class where they are declared.
Polymorphism: Polymorphism means more than one function with same name, with
different working. It can be static or dynamic. In static polymorphism memory will be
allocated at compile time. In dynamic polymorphism memory will be allocated at
runtime. Both function overloading and operator overloading are an examples of static
polymorphism. Virtual function is an example of dynamic polymorphism.
Class : Class is an encapsulation of data and coding. Classes are an expanded version of
structures. Structure can contain multiple variables. Classes can contain multiple variables,
even more, classes can also contain functions as class member. Variables available in class
are called Data Members. Functions available in class are called Member Functions.
Object : Class is a user-defined data type and object is a variable of class type. Object is used
to access class members. Classes and objects are the two main aspects of object-oriented
programming.
Look at the following illustration to see the difference between class and objects:
Another example:
Page 2 of 12
Object Oriented Programming
So, a class is a template for objects, and an object is an instance of a class. When the individual
objects are created, they inherit all the variables and functions from the class.
Declaration of class
Declaration of class must start with the keyword class followed by the class name and class
members are declared within braces.
Page 3 of 12
Object Oriented Programming
Instantiation of object means, create an object of class to access its members. Object is a
variable of class type. Class members are accessed using the dot operator(.) between class's
object and class's member name.
class-name obj;
Employee Emp;
Page 4 of 12
Object Oriented Programming
----------
body of function
----------
class_name:: A program may contain more than one class and these classes may have similar
member functions. Class_name:: tells the compiler which class the function belongs to and the
scope of the member function is restricted to the class_name.
argument list Represents the type and number of value function will take, values are sent by
the calling statement.
#include<iostream.h>
#include<conio.h>
class Employee
Page 5 of 12
Object Oriented Programming
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
};
cin>>Id;
cin>>Name;
cin>>Age;
Page 6 of 12
Object Oriented Programming
cin>>Salary;
cout<<"\n\nEmployee Id : "<<Id;
void main()
Output :
Page 7 of 12
Object Oriented Programming
Enter Employee Id : 1
Employee Id : 1
Employee Age : 29
In the above program, we have declared two member functions GetData() and PutData() within
class and statement 1 and 2 is defining these methods outside class. Statement 3 is creating an
object E of Employee class. Statement 4 and 5 is invoking/calling member function through the
object E of Employee class.
----------
body of function
----------
Page 8 of 12
Object Oriented Programming
argument list Represents the type and number of value function will take, values are sent by
the calling statement.
Definition of member function inside class is similar to defining normal function. There is no
need to tell compiler about the class the function belongs to because the definition of member
function is already in the class.
#include<iostream.h>
#include<conio.h>
class Employee
int Id;
char Name[25];
int Age;
long Salary;
public:
Page 9 of 12
Object Oriented Programming
cin>>Id;
cin>>Name;
cin>>Age;
cin>>Salary;
cout<<"\n\nEmployee Id : "<<Id;
};
Page 10 of 12
Object Oriented Programming
void main()
Output :
Enter Employee Id : 1
Employee Id : 1
Employee Age : 29
Page 11 of 12
Object Oriented Programming
In the above program, we have defined two member functions GetData() and PutData() within
class. Statement 3 is creating an object E of Employee class. Statement 4 and 5 is
invoking/calling member function through the object E of Employee class.
Page 12 of 12