C++21: Classes and Objects
Object Oriented Programming with C++
Features:
1)Data Abstraction and Encapsulation:
Abstraction: The feature indicates that here we can describe any imaginary and non imaginary entity in programming.
Encapsulation: Encapsulation means to bind some thing in a group. The ability to combine data and functions together is called as encapsulation.
2) Inheritance:
Reusability of the existing classes without distrusting the existing classes is called as inheritance. Inheritance supports to reuse existing code.
3) Polymorphism:
Polymorphism indicates similar behavior of objects of different classes types. It is a technique which is used to develop a code which will work for different objects of different classes.
Class: The class is the programmatical description of a entity or we can say that class is a blue print for creating objects.
Object: Every imaginary and non-imaginary thing(i.e. entity) to which we can describe programmatically is called as object.
Example:
#include <iostream>
using namespace std;class Rectangle{
private: int L, B;
public: void setdata(int p,int q) { L=p; B=q; }
void area() { int A= L*B; cout<<"Area is : "<<A<<endl;
} void perimenter() { int P= 2*(L+B); cout<<"Perimeter is : "<<P<<endl;
}
};int main(){ Rectangle x,y;
x.setdata(5,4); x.area(); x.perimenter();
y.setdata(5,4); y.area(); y.perimenter();
return 0;}Area is : 20
Perimeter is : 18#include <iostream>
using namespace std;class Student{ private: int RN; char SN[20],D[20];
public: void setdata(int rn,char n[], char d[]) { RN = rn; strcpy(SN,n); strcpy(D,d); }
void showdata(){ cout<<"\n Roll number = "<<RN<<endl; cout<<"Student Name = "<<SN<<endl; cout<<"Department = "<<D<<endl;
}};int main(){ //Design a class // *? Student with datamembere and name department define number functions setdat() and showdata() Student a,b; a.setdata(111,"Jack","CSE"); a.showdata();
b.setdata(122,"James","IT"); b.showdata();
return 0;}Roll number = 111
Student Name = Jack
Department = CSE
Roll number = 122
Student Name = James
Department = IT#include <iostream>
using namespace std;class Employee{
private: char EN[20]; int ES; int EA;
public:
void setdata(char n[],int s, int a) { strcpy(EN,n); ES = s; EA = a;
}
void showdata(){ cout<<"\n Name of Employee : "<<EN<<endl; cout<<"Salary of Employee : "<<ES<<endl; cout<<"Age of Employee : "<<EA<<endl; }};
main(){ Employee a,b;
a.setdata("Harry",20000,24 ); a.showdata(); b.setdata("Ayush",2000,17 ); b.showdata();
} Name of Employee : Harry
Salary of Employee : 20000
Age of Employee : 24
Name of Employee : Ayush
Salary of Employee : 2000
Age of Employee : 17#include <iostream>
using namespace std;class Distance{
private : int F; int I;
public: void setdata(int f,int i){ F =f; I = i;
} int convert(){ return(F*12+I);
} void showdata(){ cout<<"Feet: "<<F<<" Inches: "<<I<<endl; }
};
main(){ Distance a;
a.setdata(5,2); a.showdata();
int x = a.convert();
cout<<"The value in Inches is: "<<x<<endl;
}Feet: 5 Inches: 2
The value in Inches is: 62#include <iostream>
using namespace std;class Currency{
private: int R,P;
public:
void setdata(int r,int p){ R =r; P = p; }
void showdata(){
cout<<"Rupees: "<<R<<" Paise: "<<P<<endl; }
int convert(){ return (100*R + P); }};
main(){
Currency a; //Object
a.setdata(12, 50); a.showdata();
int r = a.convert();
cout<<"Ruppes in paise is : "<<r<<endl;
}Rupees: 12 Paise: 50
Ruppes in paise is : 1250#include <iostream>
using namespace std;class Rectangle{
private: int L; int B;
public: void setdata(int l, int b) { L = l; B = b; } void area() { int A = L * B;
cout << "Area of rectangle is : " << A << endl; }
void perimeter() { int P = 2 * (L + B);
cout << "The Perimeter of Rectangle is: " << P << endl; }
};
int main(){
Rectangle a;
a.setdata(5, 4); a.area(); a.perimeter();
Rectangle b;
b.setdata(10,30);
b.area(); b.perimeter();
return 0;}
The Perimeter of Rectangle is: 18
Area of rectangle is : 300
The Perimeter of Rectangle is: 80
Comments
Post a Comment