0% found this document useful (0 votes)
81 views

Chapter-3.1 (Basics of Object Oriented Programming)

The document discusses object-oriented programming using C++. It begins by explaining the differences between procedural and object-oriented programming, noting that OOP emphasizes data over procedures and divides programs into objects that communicate through functions. It then covers key concepts of OOP like class, object, encapsulation, inheritance, and polymorphism. The remainder of the document provides details on specifying classes, creating objects, defining member functions, and access specifiers in C++.

Uploaded by

subham acharya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Chapter-3.1 (Basics of Object Oriented Programming)

The document discusses object-oriented programming using C++. It begins by explaining the differences between procedural and object-oriented programming, noting that OOP emphasizes data over procedures and divides programs into objects that communicate through functions. It then covers key concepts of OOP like class, object, encapsulation, inheritance, and polymorphism. The remainder of the document provides details on specifying classes, creating objects, defining member functions, and access specifiers in C++.

Uploaded by

subham acharya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Paradigms of Computer Programming

and C++
Subject Code-CST-152
UNIT-I
Chapter-3
Topic-->Object Oriented Programming Using C++
Course Objectives
• To understand the concept of the various Programming Paradigms.
• To apply different programming languages for modeling real world
problems.

Object Oriented Programming Objectives


•To understand the basics of Object Oriented Programming.To apply
programming languages for modeling real world problems.
• To enhance the programming skills in C++ with programming concepts.
•To understand the characteristics of object oriented programming.
•To utilize the concept of class, object friend function in real world problem.

University Institute of Engineering


Contents
• Differences between object oriented and procedure oriented
programming
• Difference between structure and class
• Features of Object Oriented Programming
• Specifying a class
• Creating objects
• Accessing class members
• Defining a member function inside and outside class
• Access Specifiers
• Inline function
• Static data members & member functions
• Friend function
27-04-2019 University Institute of Engineering
Differences between object oriented & procedure
oriented programming

Procedure-oriented programming Object-oriented programming


Emphasis is on doing things. Emphasis is on data rather than procedure.
Large programs divided into smaller functions(fig.1). Programs are divided into objects.
Functions can share global data(fig.2). Functions that operate on the data of an object are tied
together in the data structure.
Data move openly. Data is hidden (fig.3).
Functions transform data from one form to another. Objects may communicate with each other through
functions.
Top-down approach in program design. Bottom-up approach in program design.

27-04-2019 University Institute of Engineering 1


Procedure-oriented programming

Fig.1. Structure of procedure-oriented programs Fig.2. Relationship of data and functions in


procedural programming.
27-04-2019 University Institute of Engineering 2
Object-oriented programming

Fig.3. Organization of data and functions in OOP

27-04-2019 University Institute of Engineering 3


Difference between Structure and Class

CLASS STRUCTURE
Way to group both data & functions Way to group related data

Reference type & its object is created on heap Value type & its object is created on stack
memory memory.
Can inherit the another class. Does not support the inheritance.
Can have the all types of constructor & Can only have the parameterized constructor.
destructor.
Member variable can be initialized directly. Member variable can not be initialized directly.

27-04-2019 University Institute of Engineering 4


Features of Object Oriented Programming

i. Object
ii. Class
iii. Data abstraction and Encapsulation
iv. Information hiding
v. Inheritance
vi. Polymorphism

27-04-2019 University Institute of Engineering 5


Object

Object is a instance of class.


Represent a person, a place, a bank account, a table of data or any item the
program has to handle.

Fig.4. Two ways of representing an object

27-04-2019 University Institute of Engineering 6


Class

Set of data & code of an object ->user-defined data type –>class.


Once a class has been defined, we can create any number of objects belonging to
that class.
A class is collection of objects of similar type.
Syntax used to create an object is no different than the syntax used to create an
integer object in C.

Example-- If fruit has been defined as a class, then the statement


fruit mango;
will create an object mango belonging to the class fruit.

27-04-2019 University Institute of Engineering 7


Data Abstraction & Encapsulation

Wrapping up of data & functions into a single unit (called class) -encapsulation.
Data is not accessible to the outside world
Functions which are wrapped in the class can access it.
Abstraction - act of representing essential features without background details.

Fig.5. Encapsulation

27-04-2019 University Institute of Engineering 8


Information Hiding
Insulation of data from direct access by the program – data or information hiding.
Attributes are - data members because they hold information.
Functions are - member functions.

Fig.6. Concept of Information Hiding

27-04-2019 University Institute of Engineering 9


Inheritance
Process by which objects of one class acquire the properties of objects of another
class.
Each derived class shares common characteristics with the class from which it is
derived(fig.7).
Inheritance provides reusability and expandability.

Fig. 7. Property Inheritance


27-04-2019 University Institute of Engineering 10
Polymorphism
Ability to take more than one form.
An operation may exhibit different behaviours in different instances.
Using a single function name to perform different types of tasks is known as
function overloading(fig.8).

Fig. 8. Polymorphism
27-04-2019 University Institute of Engineering 11
Specifying a class
Class specification has two parts:
• Class declaration
• Class function definitions
Class declaration describes type and scope of its members
Class function definition describes how class functions are implemented.
Example:
class class_name Class body contains the
Declaration of variables
{ private: variable declarations; And functions.
public: function declaration;
};

COLON
27-04-2019 University Institute of Engineering 12
Creating objects
For utilizing the defined class, we need variables of the class type. For example,
Largest ob1,ob2; //object declaration will create two objects ob1 and ob2 of
largest class type.
Memory space is allocated separately to each object for their data members.
Member variables store different values for different objects of a class.

27-04-2019 University Institute of Engineering 13


Accessing class members
class person { //class declaration
public: //access specifier
string name; //variable declaration
int number;
};
main() { //main function
person obj; //object creation for class
cout<<“Enter name:”; //get input values for object variables
cin>>obj.name;
cout<<“Enter number:”;
cin>>obj.number;
cout<<obj.name<<“:”<<obj.number<<endl; }
27-04-2019 University Institute of Engineering 14
Defining a member function inside and outside class

There are two ways:


1. Member functions defined inside class
• Do not need scope resolution operator, class name.
2. Member functions defined outside class
• Using Binary scope resolution operator (::)
• “Ties” member name to class name
• Uniquely identify functions of particular class
• Different classes can have member functions with same name

Format for defining member functions outside the class is:


returnType ClassName::MemberFunctionName( )
{

}
27-04-2019 University Institute of Engineering 15
Access Specifiers
PUBLIC PRIVATE PROTECTED
Data members and member No one can access the class Makes class member
functions declared public can members declared private inaccessible outside the class.
be accessed by other classes outside that class. But they can be accessed by
too Compile time error any subclass of that class.
class PublicAccess { class PrivateAccess { class ProtectedAccess {
public: private: protected:
int x; int x; int x;
// Data Member // Data Member // Data Member
Declaration Declaration Declaration
void display(); void display(); void display();
// Member Function // Member Function // Member Function
decaration decaration decaration
} } }
27-04-2019 University Institute of Engineering 16
Inline function

An inline function is expanded (i.e. the function code is replaced


when a call to the inline function is made) in the line where it is
invoked.
Syntax:
inline function_header
{
body of the function
}

27-04-2019 University Institute of Engineering 17


Static data members
Used to store value common to the whole class. The static data
member differs from an ordinary data member in the following
ways :
(i) Only a single copy of the static data member is used by all the
objects.
(ii) It can be used within the class but its lifetime is the whole program.

For making a data member static, we require :


(a) Declare it within the class.
(b) Define it outside the class.

27-04-2019 University Institute of Engineering 18


Static data members
For example
Class Student
{
Static int count; //declaration within class
-----------------
};
The static data member is defined outside the class as :
int student :: count; //definition outside class
We can also initialize the static data member at the time of its definition as:
int student :: count = 0;
If we define three objects as : student obj1, obj2, obj3;

27-04-2019 University Institute of Engineering 19


Static member functions

A static member function can access only the static members of a class.
In C++, a static member function fifers from the other member functions in the
following ways:
(i) Only static members (functions or variables) of the same class can be accessed
by a static member function.
(ii) It is called by using the name of the class rather than an object as given
below:
Name_of_the_class :: function_name
For example:
student::showcount();

27-04-2019 University Institute of Engineering 20


Friend functions

Non-member function (not a member function of a class).


It is declared inside the class with friend keyword.
It must be defined outside the class to which it is friend.
Friend function can become friend to more then one class.
Friend function cannot access data members of class directly.

27-04-2019 21
University Institute of Engineering
Friend functions Example
Eg.
class B;
class A void fun(A ob, B obj)
{
{ public:
cout<<"sum"<<ob.a+obj.b;
void set() }
{ } void main()
{
friend void fun(A,B);
A ob;
}; B obj;
class B ob.set( );
obj.set( );
{ public:
fun(ob,obj);
void set( ) getch();
{ } }
friend void fun(A,B);
};
27-04-2019 22
University Institute of Engineering
References

i. https://books.google.co.in/books?isbn=0070669074
ii. E Balagurusamy., “Object Oriented Programming in C++”, Tata McGraw-Hill.
iii. https://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm
iv. http://www.studytonight.com/cpp/class-and-objects.php
v. http://www.cplusplus.com/doc/tutorial/classes/

27-04-2019
University Institute of Engineering
Unit Course Outcomes

•Understand the various paradigms of computer programming


• Identify the strengths and weaknesses of different programming paradigms
I-III
•To provide in-depth knowledge of various concepts of programming
paradigms

Object Oriented Programming Outcomes


•To apply the concept of class and objects in various programming problems.
• Work with the characteristics of an object-oriented programming language in a
program.
• Use the basic object-oriented principles in program problem solving techniques.
•Write Program with basic features of the C++ programming language.

You might also like