0% found this document useful (0 votes)
16 views18 pages

OPPs CPP Coading With Harry

The document discusses the importance of Object-Oriented Programming (OOP) in C++, highlighting its advantages over Procedure Oriented Programming (POP) such as improved readability, maintainability, and data security through the use of classes and objects. It outlines key OOP concepts like inheritance, polymorphism, and encapsulation, and explains the differences between classes and structures in terms of data access and security. Additionally, it provides examples of class implementation, access modifiers, and the role of friend functions in accessing private data.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

OPPs CPP Coading With Harry

The document discusses the importance of Object-Oriented Programming (OOP) in C++, highlighting its advantages over Procedure Oriented Programming (POP) such as improved readability, maintainability, and data security through the use of classes and objects. It outlines key OOP concepts like inheritance, polymorphism, and encapsulation, and explains the differences between classes and structures in terms of data access and security. Additionally, it provides examples of class implementation, access modifiers, and the role of friend functions in accessing private data.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Why Object-Oriented Programming?

 C++ language was designed with the main intention of adding object-oriented
programming to C language
 As the size of the program increases readability, maintainability, and bug-free
nature of the program decrease.
 This was the major problem with languages like C which relied upon functions or
procedure (hence the name procedural programming language)
 data security was easily compromised
 Using classes solves this problem by modeling program as a real-world scenario
 C++ --> initially called --> C with classes by stroustroup

Difference between Procedure Oriented Programming and Object-


Oriented Programming
Procedure Oriented Programming POP
 Consists of writing a set of instructionCprogramming
for the computer to follow
 The main focus is on functions and not on the flow of data
 Functions can either use local or global data
 Data moves openly from function to function

Object-Oriented Programming
 Works on the concept of classes and object
 A class is a template to create objects
 Treats data as a critical element.
Restrictions nobody an access or Rules
applied

 Such as and
Decomposes the problem in objects Public, Private,
builds Protected
data and functions around the
objects.

Basic Concepts in Object-Oriented Programming


 Classes - Basic template for creating objects
 Objects – Basic run-time entities
 Data Abstraction & Encapsulation – Wrapping data and functions into a single
unit

 Inheritance – Properties of one class can be inherited into others.


o (Editing one’s template by another template)
o Improves readability, reusability
 Polymorphism – Ability to take more than one forms.
o e.g. Oliver queen, Green Arrow are same persons.
o Operator Overloading
 Dynamic Binding – Code which will execute is not known until the program runs
 Message Passing – Object.message (Information) call format.

Benefits of Object-Oriented Programming


 Better code reusability using objects and inheritance.
 Principle of data hiding helps build secure systems.
 Multiple Objects can co-exist without any interference.
 Software complexity can be easily managed.

Why use classes instead of structures


Classes and structures are somewhat the same but still, they have some
differences. For example, we cannot hide data in structures which means
that everything is public and can be accessed easily which is a major
drawback of the structure because structures cannot be used where data
security is a major concern. Another drawback of structures is that we
cannot add functions in it.
Classes in C++
Classes are user-defined data-types and are a template for creating
objects. Classes consist of variables and functions which are also called class
members.
Public Access Modifier in C++
All the variables and functions declared under public access modifier will be
available for everyone. They can be accessed both inside and outside the
class. Dot (.) operator is used in the program to access public data members
directly.
Private Access Modifier in C++
All the variables and functions declared under a private access modifier can
only be used inside the class. They are not permissible to be used by any
object or function outside the class.
An example program to demonstrate classes, public and private access

#include<iostream>
using namespace std;
class Animal
{
private :
int a,b,c;

public :
int d,e;
void display()
{
cout<<"a : "<<a<<endl;
cout<<"b : "<<b<<endl;
cout<<"c : "<<c<<endl;
cout<<"d : "<<d<<endl;
cout<<"e : "<<e<<endl;
}
void getabc(int a1,int b1, int c1);

};
void Animal :: getabc(int a1,int b1,int c1)
{
a=a1;
b=b1;
c=c1;
}
int main()
{
Animal dog;
// dog.a=10; private not allowed but can be accept by function
dog.d=50;
dog.e=60;
dog.getabc(10,20,30); // private accessing by function
dog.display();
return 0;
}

#include<iostream>
using namespace std;
class Animal
{
private :
int a,b,c;

public :
int d,e;
void display()
{
cout<<"a : "<<a<<endl;
cout<<"b : "<<b<<endl;
cout<<"c : "<<c<<endl;
cout<<"d : "<<d<<endl;
cout<<"e : "<<e<<endl;
}
void getabc(int a1,int b1, int c1)
{
a=a1;
b=b1;
c=c1;
}
};
int main()
{
Animal dog;
// dog.a=10; private not allowed but can be accept by function
dog.d=50;
dog.e=60;
dog.getabc(10,20,30); // private accessing by function
dog.display();
return 0;
}
Using Array in Class

]
Or
Using Array in Objects
Static Data Member
 Default value-> 0, Visibility-> Class, Life-> Till the end of Program
 Declare only once but can run till end
 Storage Data Section
 Sataic sare object share karta.

Static Function
Properties of friend functions
Friend functions are those functions that have the right to access the private data
members of class even though they are not defined inside the class.
1. Not in the scope of class
In C++, a friend function is a special function that is not a member of a class but is
granted access to the private and protected members of that class.
It can be used to access and manipulate the private data of a class without violating
the encapsulation principle.
Friend functions are typically declared inside a class but are defined outside of it.
Here's how friend functions work and how to declare and use them:

You might also like