Docsity

OOP Concepts in C++: Inheritance, Encapsulation, Polymorphism, Study notes of Object Oriented Programming

Toggle sidebar
bg1
INHERITANCE PROGRAM
#include<iostream.h>
#include<conio.h>
class student
{
public:
int reg_no;
//float per;
char name[20];
void getdata()
{
cout<<"Enter Reg_No :- \t";
cin>>reg_no;
cout<<"Enter Name :- \t";
cin>>name;
}
};
class marks : public student
{
public:
int m1,m2,m3,tot;
float per;
void getmarks()
{
getdata();
cout<<"Enter Marks 1 :- \t";
cin>>m1;
cout<<"Enter Marks 2 :- \t";
cin>>m2;
cout<<"Enter Marks 3 :- \t";
cin>>m3;
tot=m1+m2+m3;
return tot;
}
void display()
{
getmarks();
cout<<"Roll Not \t Name \t Marks1 \t marks2 \t Marks3 \t Total
\t Percentage";
cout<<rno<<"\t"<<name<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<tot<<"\t"<<
per;
}
};
void main()
{
student std;
std.getmarks();
std.display();
}
pf3
pf4

Partial preview of the text

Download OOP Concepts in C++: Inheritance, Encapsulation, Polymorphism and more Study notes Object Oriented Programming in PDF only on Docsity!

INHERITANCE PROGRAM

#include<iostream.h> #include<conio.h> class student { public: int reg_no; //float per; char name[20]; void getdata() { cout<<"Enter Reg_No :- \t"; cin>>reg_no; cout<<"Enter Name :- \t"; cin>>name; } }; class marks : public student { public: int m1,m2,m3,tot; float per; void getmarks() { getdata(); cout<<"Enter Marks 1 :- \t"; cin>>m1; cout<<"Enter Marks 2 :- \t"; cin>>m2; cout<<"Enter Marks 3 :- \t"; cin>>m3; tot=m1+m2+m3; return tot; } void display() { getmarks(); cout<<"Roll Not \t Name \t Marks1 \t marks2 \t Marks3 \t Total \t Percentage"; cout<<rno<<"\t"<<name<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<tot<<"\t"<< per; } }; void main() { student std; std.getmarks(); std.display(); }

ENCAPSUALTION

#include using namespace std; class Adder{ public: // constructor Adder(int i = 0 ) { total = i; } // interface to outside world void addNum(int number) { total += number; } // interface to outside world int getTotal() { return total; }; private: // hidden data from outside world int total; }; int main( ) { Adder a; a.addNum( 10 ); a.addNum( 20 ); a.addNum( 30 ); cout << "Total " << a.getTotal() <<endl; return 0 ; } POLYMORPHISM #include using namespace std; class Shape { protected: int width, height; public: Shape( int a= 0 , int b= 0 ) { width = a; height = b; } int area() { cout << "Parent class area :" <<endl; return 0 ; } }; class Rectangle: public Shape{ public:

int main( ) { Adder a; a.addNum( 10 ); a.addNum( 20 ); a.addNum( 30 ); cout << "Total " << a.getTotal() <<endl; return 0 ; }

FUNCTION OVERLOADING

#include using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print( 5 ); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0 ; }