Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

62.Write a C++ program to demonstrate virtual function.

Program :-
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print()
{
cout << "Base Function" << endl;
}
};
class Derived : public Base
{
public:
void print()
{
cout << "Derived Function" << endl;
}
};

int main()
{
Derived derived1;
// pointer of Base type that points to derived1
Base* base1 = &derived1;
// calls member function of Derived class
base1->print();
return 0;
}
Output :-
63. Write a C++ program to demonstrate pure virtual function having
base class name “shape” which contains dummy function name “area”
which has null body and two child classes inherits “shape” class named
“triangle” And “rectangle” . both child classes redefined “shape” class
dummy function to find area’a of tringle and rectangle respectively.
Program :-
#include <iostream>
class Shape
{
public:
// pure virtual function with a null body
virtual double area() = 0;
};
class Triangle : public Shape
{
private:
double base;
double height;
public:
Triangle(double base, double height) : base(base), height(height) {}
double area() override
{
return 0.5 * base * height;
}
};
class Rectangle : public Shape
{
private:
double width;
double height;
public:
Rectangle(double width, double height) : width(width), height(height) {}

double area() override


{
return width * height;
}
};
int main()
{
Triangle triangle(10, 20);
std::cout<< "Area of triangle: " <<triangle.area() << std::endl;
Rectangle rectangle(10, 20);
std::cout<< "Area of rectangle: " <<rectangle.area() << std::endl;
return 0;
}

Output :-
64. Write a C++ program to overload unary + operator using member
function.
Program :-
#include <iostream>
using namespace std;
class A
{
private:
int a,b;
public:
A(int x , int y)
{
a = x;
b = y;
cout<<"before overloading a and b is:"<<a<<b<<endl;
}
void operator -()
{
a = -a;
b = -b;
cout<<"\nvalue of a and b:"<<a<<b;
}

};
int main()
{
A n(10,20);
-n;
return 0;
}
Output :-
65. Write a C++ program to overload unary + operator using friend
function.
Program :-
#include <iostream>
using namespace std;
class A
{
private:
int a,b;
public:
A(int x , int y)
{
a = x;
b = y;
cout<<"before overloading a and b is:"<<a<<b<<endl;
}
friend void operator -(A o)
{
o.a = -o.a;
o.b = -o.b;
cout<<"\nvalue of a and b:"<<o.a<<o.b;
}
};
int main()
{
A o(10,20);
-o;
return 0;
}
Output :-
66. Write a C++ program to overload unary ++ operator using member
function.
Program :-
#include <iostream>
using namespace std;
class A
{

public:
void operator ++()
{
int a = 8;
cout<<"a is:"<<a<<endl;
a=++a;
cout<<"pre increament value of a:"<<a<<endl;

int b;
b=a++;
cout<<"value of b is:"<<b<<endl;
}
};
int main()
{
A A;
++A;

}
Output :-
67. Write a C++ program to overload binary + operator using member
function for addition of two complex number with member function.
Program :-
#include<iostream>
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
Complex operator+(Complex obj)
{
Complex c;
c.num1=num1+obj.num1;
c.num2=num2+obj.num2;
return(c);
}
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
int main()
{
Complex c1, c2, sum;
c1.accept();
c2.accept();
sum = c1+c2;
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display();
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display();
return 0;
}
Output :-
68. Write a C++ program to overload binary + operator using member
function for addition of two complex number with friend function.
Program :-
#include<iostream>
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
//Overloading '+' operator using Friend function
friend Complex operator+(Complex c1, Complex c2);

void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
Complex operator+(Complex c1, Complex c2)
{
Complex c;
c.num1=c1.num1+c2.num1;
c.num2=c1.num2+c2.num2;
return(c);
}
int main()
{
Complex c1,c2, sum;
c1.accept();
c2.accept();
sum = c1+c2;
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display();
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display();
return 0;
}

Output :-

You might also like