Professional Documents
Culture Documents
class Room {
private:
double length;
double breadth;
public:
// 1. Constructor with no arguments
Room() {
length = 6.9;
breadth = 4.2;
}
double calculateArea() {
return length * breadth;
}
};
int main() {
Room room1, room2(8.2, 6.6), room3(8.2);
cout << "\nWhen breadth is fixed to 7.2 and (8.2) is passed:" << endl;
cout << "Area of room = " << room3.calculateArea() << endl;
return 0;
}
2.
// C++ program to illustrate
// Constructor overloading
#include <iostream>
using namespace std;
class construct
{
public:
float area;
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
// of class name
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 1;
}
3.
operator overloading
#include
#include
#include
class Myclass {
int x;
public:
friend void* operator new(size_t size);
friend void operator delete(void*);
Myclass(int i) {
x = i;
cout << "Constructor called \n";
}
~Myclass() { cout << "Destructor called \n"; }
};
int main()
{
Myclass *m = new Myclass(5);
delete m;
return 0;
}
4.
#include<iostream>
#include<conio.h>
using namespace std;
class Complex
{
int real, imag;
public:;
Complex(int r=5, int i=8)
{
real = r;
imag = i;
}
void print()
{
cout << real << "+" << imag << "i" << endl;
}
};
int main()
{
Complex c1(3, 2);
Complex c2(4, 0);
Complex c3;
c3 = c1 ^ c2;
c3.print();
int x = 1;
int y = 3;
int z = x + y;
cout << "z value is: " << z << endl;
return 0;