OPPs CPP Coading With Harry
OPPs CPP Coading With Harry
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
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.
#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: