0% found this document useful (0 votes)
33 views20 pages

Gcek Confidential

The document discusses various object-oriented programming concepts in C++ including operator overloading, inheritance, polymorphism and function overloading. It provides code examples for binary, logical, relational and unary operator overloading. Examples of different types of inheritance like multilevel, multiple, hierarchical and hybrid inheritance are also given.

Uploaded by

Psyduck
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)
33 views20 pages

Gcek Confidential

The document discusses various object-oriented programming concepts in C++ including operator overloading, inheritance, polymorphism and function overloading. It provides code examples for binary, logical, relational and unary operator overloading. Examples of different types of inheritance like multilevel, multiple, hierarchical and hybrid inheritance are also given.

Uploaded by

Psyduck
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/ 20

16.

BINARY OPERATOR OVERLOADING :


#include<iostream>
using namespace std ;
class complex
{
public :
int real;
int img;
complex()
{
real = 0;
img = 0;
}
complex(int x, int y)
{
real=x;
img = y;
}
friend complex operator + (const complex & A,const complex &B);
void display()
{
cout<<"Complex No. :"<<real<<"+"<<img<<"i"<<endl;
}
};
complex operator + (complex &A,complex &B)
{
complex temp;
temp.real=A.real+B.real;
temp.img=A.img+B.img;
return temp;
}
int main()
{
complex obj1(2,3);
complex obj2(4,5);
complex res = obj1 + obj2;
res.display();
return 0;

}
13.NEW AND DELETE OPERATOR OVERLOADING:
#include<iostream>
#include<stdlib.h>
using namespace std;
class student
{
string name;
int age;
public:
student()
{
cout<< "Constructor is called\n" ;
}
student(string name, int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size << endl;
void * p = malloc(size); will also work fine

return p;
}
void operator delete(void * p)
{
cout<< "Overloading delete operator " << endl;
free(p);
}
};
int main()
{
student * p = new student("Yash", 24);
p->display();
delete p;
}
14.FUNCTION OVERLOADING :
#include<iostream>
using namespace std;
class A
{
public:
int ans,a,b,c;
void add(int x,int y)
{
a=x,b=y;
ans=x+y;
}
void add(int x,int y,int z)
{
a=x,b=y,c=z;
ans = x+y+z;
}
void display()
{
cout<<"Addition of "<<a<<" and "<<b<<" is = "<<ans<<endl;
cout<<"Addition of "<<a<<","<<b<<" and "<<c<<" is = "<<ans<<endl;
}
};
int main()
{
A a;
a.add(23,11);
a.add(34,54,2);
a.display();
return 0;
}
19.INLINE FUNCTION :
#include<iostream>
#include<stdlib.h>
using namespace std;
inline int add(int a,int b)
{
return(a+b);
}
inline int sub(int a,int b)
{
return(a-b);
}
inline int mul(int a,int b)
{
return(a*b);
}
inline float division(int a,int b)
{
return(a/b);
}
int main()
{
int a,b,s;
char c;
float ans;
do
{
cout<<"Enter expression :";
cin>>a>>c>>b;
switch(c)
{
case '+' : ans=add(a,b);
break;
case '-' : ans=sub(a,b);
break;
case '*' : ans=mul(a,b);
break;
case '/' : ans=division(a,b);
break;
default : cout<<"Invalid input.";
}
cout<<"Answer :"<<ans<<endl;
cout<<"Press 1 for continue / for skip press 0. :";

cin>>s;

}while (s==1);
return 0;
}
18.LOGICAL OPERATOR OVERLOADING :
#include <iostream>

class MyClass {
private:
bool condition;

public:
MyClass(bool cond)
{
condition = cond;
}

// Overloading the logical AND operator (&&)


MyClass operator&&( MyClass& other)
{
return (this->condition && other.condition);
}

// Overloading the logical NOT operator (!)


MyClass operator!() const {
return (!this->condition);
}

// Function to display the state of the object


void display() const {
if (condition) {
std::cout << "True\n";
} else {
std::cout << "False\n";
}
}
};

int main() {
MyClass obj1(true);
MyClass obj2(false);

// Using the overloaded logical AND operator


MyClass result1 = obj1 && obj2;
result1.display(); // Output: False
// Using the overloaded logical NOT operator
MyClass result2 = !obj1;
result2.display(); // Output: False

return 0;
}
17.RELATIONAL OPERATOR OVERLOADING :
#include<iostream>
#include<stdbool.h>
using namespace std;
class A
{
public :
int a,b;
A(int a)
{
this->a=a;
}
bool operator < (A & s)
{
if(this->a < s.a)
return true;
}
};
int main()
{
A x(2);
A x1(3);
if(x<x1)
{
cout<<x.a<<"is less than "<<x1.a;
}

}
20.UNARY OPERATOR OVERLOADING USING FRIEND FUNCTION :
#include<iostream>
using namespace std;
class trial
{
public :
int a,b;
trial(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"Value of a :"<<a<<endl;
cout<<"Value of b :"<<b<<endl;
}
friend void operator - (trial & A);
};
void operator -(trial & A)
{
A.a=-A.a;
A.b=-A.b;
}
int main()
{
trial obj(34,-3);
obj.show();
-obj;
obj.show();
return 0;
}
15.UNARY OPERATOR OVERLOADING :
#include<iostream>
using namespace std;
class trial
{
public :
int a,b;
trial(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"Value of a :"<<a<<endl;
cout<<"Value of b :"<<b<<endl;
}
friend void operator - (trial & A);
};
void operator -(trial & A)
{
A.a=-A.a;
A.b=-A.b;
}
int main()
{
trial obj(34,-3);
obj.show();
-obj;
obj.show();
return 0;
}

8.MULTILEVEL INHERITANCE :
#include<iostream>
using namespace std;
class Landline_phone{ //Base class
public:
void call()
{
cout<<"Calling from landline phone"<<endl;
}
};
class Cell_phone:public Landline_phone{ //Intermediate class
public:
void SMS()
{
cout<<"Sending SMS through cell phone"<<endl;
}
};
class Smart_phone:public Cell_phone{ //Derived class
public:
void internet()
{
cout<<"Video call from smartphone by internet access"<<endl;
}
};
int main()
{
Smart_phone obj;
obj.call();
obj.SMS();
obj.internet();
return 0;
}

10.HYBRID INHERITANCE :

#include <iostream>
using namespace std;
// Base class: Student
class Student {
public:
int RegNo;
void setRegNo(int roll) {
RegNo = roll;
}
};
// First derived class: Test (inherits from Student)
class Test : public Student {
public:
int academicMarks;
void setAcademicMarks(int marks) {
academicMarks = marks;
}
};
// Second derived class: Sport
class Sport {
public:
int sportPoints;
void setSportPoints(int points) {
sportPoints = points;
}
};
// Derived class from Test and Sport: Result
class Result : public Test, public Sport {
public:
void displayAnnualResult() {
cout << "Reg No: " << RegNo << endl;
cout << "Academic Marks: " << academicMarks << endl;
cout << "Sport Points: " << sportPoints << endl;
// Calculate and display the annual result ( average of marks and points)
int total = academicMarks + sportPoints;
double average = total / 2;
cout << "Annual Result (Average): " << average << endl;
}
};
int main() {
// Create an object of Result
Result studentResult;
// Set student details
studentResult.setRegNo(50);
studentResult.setAcademicMarks(85);
studentResult.setSportPoints(80);
// Display the annual result
cout << "Student Annual Result:" << endl;
studentResult.displayAnnualResult();
return 0;
}

9.MULTIPLE INHERITANCE :

#include <iostream>
using namespace std;
// Base class: CentralGovernment
class CentralGovernment {
public:
int centralfund;
void get_CentralFund(int Cfund) {
cout << "Central Government Fund obtained."<<Cfund << endl;
centralfund=Cfund;
}
};
// Base class: StateGovernment
class StateGovernment {
public:
int statefund;
void getStateFund(int Sfund) {
cout << "State Government Fund obtained."<<Sfund << endl;
statefund=Sfund;
}
};
// Derived class: Corporation (inherits from CentralGovernment and StateGovernment)
class Corporation : public CentralGovernment, public StateGovernment {
public:
int localfund;
void getLocalFund(int Lfund)
{
cout << "Local Corporation Fund obtained." <<Lfund<< endl;
localfund=Lfund;
int totalfund=centralfund+statefund+localfund;
cout<<"Total fund:"<<totalfund<<endl;
}
};
int main() {
// Create an object of Corporation
Corporation obj;
// Demonstrate accessing methods from derived class
obj.get_CentralFund(400000);
obj.getStateFund(200000);
obj.getLocalFund(100000);
cout << endl;
}

11.HIERARCHICAL INHERITANCE :
#include <iostream>
using namespace std;
// Base class: Institution
class Institution {
public:
void education() {
cout << "Provides education." << endl;
}
};
// Derived class from Institution: School
class School : public Institution {
public:
void schoolType() {
cout << "School offers primary and secondary education." << endl;
}
};
// Derived classes from School: HindiMediumSchool and EnglishMediumSchool
class HindiMediumSchool : public School {
public:
void languageMedium() {
cout << "School conducts education in Hindi medium." << endl;
}
};
class EnglishMediumSchool : public School {
public:
void languageMedium() {
cout << "School conducts education in English medium." << endl;
}
};
// Derived class from Institution: College
class College : public Institution {
public:
void collegeType() {
cout << "College offers higher education." << endl;
}
};
// Derived classes from College: ScienceCollege, CommerceCollege, and
ArtsCollege
class ScienceCollege : public College {
public:
void stream() {
cout << "College offers science stream courses." << endl;
}
};
class CommerceCollege : public College {
public:
void stream() {
cout << "College offers commerce stream courses." << endl;
}
};
class ArtsCollege : public College {
public:
void stream() {
cout << "College offers arts stream courses." << endl;
}
};
// Derived classes from Institution: MPSCExam and UPSCExam
class MPSCExam : public Institution {
public:
void examType() {
cout << "Conducts Maharashtra Public Service Commission (MPSC)
exams." << endl;
}
};
class UPSCExam : public Institution {
public:
void examType() {
cout << "Conducts Union Public Service Commission (UPSC) exams." <<
endl;
}
};
int main() {
// Create objects to demonstrate hierarchical inheritance
cout << "Schools:" << endl;
HindiMediumSchool hindiSchool;
hindiSchool.education();
hindiSchool.schoolType();
hindiSchool.languageMedium();
cout << endl;
EnglishMediumSchool englishSchool;
englishSchool .education();
englishSchool.schoolType();
englishSchool.languageMedium();
cout << endl;
cout << "Colleges:" << endl;
ScienceCollege scienceCollege;
scienceCollege.education(); // Access inherited method
scienceCollege.collegeType();
scienceCollege.stream();
cout << endl;
CommerceCollege commerceCollege;
commerceCollege.education(); // Access inherited method
commerceCollege.collegeType();
commerceCollege.stream();
cout << endl;
ArtsCollege artsCollege;
artsCollege.education(); // Access inherited method
artsCollege.collegeType();
artsCollege.stream();
cout << endl;
cout << "Exams:" << endl;
MPSCExam mpscExam;
mpscExam.education(); // Access inherited method
mpscExam.examType();
cout << endl;
UPSCExam upscExam;
upscExam.education(); // Access inherited method
upscExam.examType();
cout << endl;
return 0;
}

7.SINGLE INHERITANCE :

#include <iostream>
using namespace std;
// Base class: Point
class Point {
public:
int x, y;
// Method to display the point's coordinates
void displayCoordinates() {
cout << "Point coordinates: (" << x << ", " << y << ")" << endl;
}
// Method to determine the quadrant of the point
void determineQuadrant() {
if (x > 0 && y > 0) {
cout << "Point lies in the first quadrant" << endl;
} else if (x < 0 && y > 0) {
cout << "Point lies in the second quadrant" << endl;
} else if (x < 0 && y < 0) {
cout << "Point lies in the third quadrant" << endl;
} else if (x > 0 && y < 0) {
cout << "Point lies in the fourth quadrant" << endl;
} else {
cout << "Point is at the origin" << endl;
}
}
};
// Derived class: CartesianCoordinate (inherits from Point)
class CartesianCoordinate : public Point {
public:
// Method to set x and y coordinates
void setCoordinates(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;
}
// Method to display x and y coordinates
void displayXYZ() {
cout << "x = " << x << ", y = " << y << endl;
}
};
int main() {
// Create an object of CartesianCoordinate
CartesianCoordinate obj;
// Set coordinates
obj.setCoordinates(2, 3);
// Call methods of the derived class
obj.displayXYZ(); // Display x and y coordinates
obj.determineQuadrant(); // Determine in which quadrant the point lies
obj.displayCoordinates(); // Display the point's coordinates
return 0;
}

6.FRIEND CLASS :
//Addition of two numbers using friend class
#include<iostream>
using namespace std;
class A
{
int a=10,b=20;
public:
void show()
{
cout<<”a =”<<a<<"b= "<<b<<endl;
}
friend class B; //friend class decleration
};
class B
{
public:
void add(A r) //reference variable create
{
int add =r.a+r.b;
cout<<"Sum of a & b is:"<<add;
}
};
int main()
{
A obj;
B obj1;
obj.show();
obj1.add(obj);
return 0;
}

5.FRIEND FUNCTION :

// C++ program to create a global function as a friend


// function of some class
#include <iostream>
using namespace std;

class base {
private:
int private_variable;

protected:
int protected_variable;

public:
base()
{
private_variable = 10;
protected_variable = 99;
}

// friend function declaration


friend void friendFunction(base& obj);
};

// friend function definition


void friendFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}

// driver code
int main()
{
base object1;
friendFunction(object1);

return 0;
}

2.PARAMETERISED CONSTRUCTOR :

#include<iostream>
using namespace std;
class A
{
public:
int ans,a,b,c;

A(int x,int y,int z)


{
a=x,b=y,c=z;
ans = x+y+z;
}
void display()
{

cout<<"Addition of "<<a<<","<<b<<" and "<<c<<" is = "<<ans<<endl;


}
};
int main()
{
A a(23,34,45);

a.display();
return 0;
}

4.DESTRUCTOR :
#include<iostream>
using namespace std;
class A
{
public:
int ans,a,b,c;
A(int x,int y,int z)
{
a=x,b=y,c=z;
ans = x+y+z;
}
void display()
{

cout<<"Addition of "<<a<<","<<b<<" and "<<c<<" is = "<<ans<<endl;


}
~A()
{
cout<<"Destructor is invoked.\n";
}
};
int main()
{
A a(23,34,45);

a.display();
return 0;
}

1.COPY CONSTRUCTOR :
#include<iostream>
using namespace std;
class A
{
public:
int ans,a,b,c;

A(int x,int y,int z)


{
a=x,b=y,c=z;
ans = x+y+z;
}
void display()
{

cout<<"Addition of "<<a<<","<<b<<" and "<<c<<" is = "<<ans<<endl;


}
A(A&r)
{
int p,q,s;
p=r.a;
q=r.b;
s=r.c;
cout<<"value of p q and r :"<<p<< " "<<q<<" "<<s<<endl;

}
};
int main()
{
A a(23,34,45);
A a1(a);

a.display();
return 0;
}

3.constructor overloading :

#include<iostream>
using namespace std;
class A
{
public:
int ans,a,b,c;
A(int x,int y)
{
a=x,b=y;
ans=x+y;
}
A(int x,int y,int z)
{
a=x,b=y,c=z;
ans = x+y+z;
}
void display()
{
cout<<"Addition of "<<a<<" and "<<b<<" is = "<<ans<<endl;
cout<<"Addition of "<<a<<","<<b<<" and "<<c<<" is = "<<ans<<endl;
}
};
int main()
{

A a (23,11);
A b (34,54,2);
a.display();
b.display();
return 0;
}

12.VIRTUAL BASE CLASS :

#include <iostream>
using namespace std;

class A {
public:
int a;
A() // constructor
{
a = 10;
}
};

class B : public virtual A {


};

class C : public virtual A {


};

class D : public B, public C {


};

int main()
{
D object; // object creation of class d
cout << "a = " << object.a << endl;

return 0;
}

21. CALCULATOR USING SWITCH CASE :

#include <iostream>

using namespace std;

class Calculator {
public:
void runCalculator() {
char operation;
double num1, num2;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

cout << "Enter operation (+, -, *, /): ";


cin >> operation;

switch(operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if(num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error: Division by zero!" << endl;
break;
default:
cout << "Error: Invalid operation!" << endl;
}
}
};

int main() {
Calculator calc;
calc.runCalculator();
return 0;
}

22. CLASS AND OBJECT :


#include <iostream>
using namespace std;

class Person {
public:
string name;
int age;
void introduce() {
cout << "Hello, my name is " << name << " and I am " << age << " years old." << endl;
}
};

int main() {
Person p1 = {"John", 30}, p2 = {"amar", 25};
p1.introduce();
p2.introduce();
return 0;
}

You might also like