0% found this document useful (0 votes)
85 views

C++ Program 1

The document contains 12 programming problems involving concepts like classes, functions, operators, constructors, and destructors in C++. Program 7 asks to write a program to perform arithmetic operations on complex numbers using operator overloading. It involves defining a Complex class with real and imaginary parts, overloading the + and - operators to add and subtract complex numbers, and displaying the results.

Uploaded by

Nitasha sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

C++ Program 1

The document contains 12 programming problems involving concepts like classes, functions, operators, constructors, and destructors in C++. Program 7 asks to write a program to perform arithmetic operations on complex numbers using operator overloading. It involves defining a Complex class with real and imaginary parts, overloading the + and - operators to add and subtract complex numbers, and displaying the results.

Uploaded by

Nitasha sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

PROGRAM-1

Write a program to print the area of


circle having radius 20.
#include<iostream>
using namespace std;
int main()
{
int r=20;
float area;
area=3.14*r*r;
cout<<”the area of circle is:-“<<area;
}
PROGRAM-2
Write a program to calculate the area
and perimeter of a rectangle whose
length and breadth are entered by the
user.
#include<iostream>
Int main()
{
Int l,b,area,peri;
Cout<<”\n enter the length of
rectangle:-“;
Cin>>l;
Cout<<”\n enter the breadth of
rectangle:-“;
cin>>b;
*(l+b);
Cout<<”\n area of rectang
area=l*b;
peri=2le is:-<<area;
cout<<”\n perimeter of rectangle
is:-“<<peri;
}

PROGRAM-3
Write a program to calculate the
addition of two numbers and
multiplication of two numbers by
using two functions in a class.
#include<iostream>
using namespace std;
class student
{
public
int a,b;
void input()
{
cout<<”\n enter the value of a:-“;
cin>>a;
cout<<”\n enter the value of B:-;
}
void addition()
{
cout<<”\n add A and B:-“<<a*b;
}
};
int main()
{
student s1;
s1.input();
s1.addition();
s1.muliply();
}

PROGRAM-4
Write a program to calculate the
factorial of a number by using class.
#include<iostream>
using namespace std;
class calculate
{
public:
int n,fact=1,i;
void input()
{
cout<<”\n enter the number:-“;
cinn>>n;
}
void factorial()
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<”\n Factorial of number is;’<<
fact;
}
};
int main()
{
calculate c1;
c1.input();
c1.factorial();
}

PROGRAM-5
Write a program to input length and
breadth using function in class,then
calculate area and perimeter in
second function of class and then
print both area and perimeter using
third function of class.
#include<iostream>
using namespace std;
class rectangle
{
public:
int l,b,area,peri;
void input()
{
cout<<”\n enter the length:-“;
cin>>l;
cout<<”\n enter the breadth:-“;
cin>>b;
}
void calculate()
{
area=l*b;
peri=2*(l+b);
}
void display()
{
cout<<”\n enter the area is: “<,area;
cout<<”\n perimeter is:”<<peri;
}
};
int main()
{
rectangle r1;
r1.input();
r2.calculated();
r3.display();
}
PROGRAM-6
Write a program to illustrate the
concept of increment operator.
#include<iostream>
using namespace std;
int main()
{
int a=21;
int c;
//Value of a will not be increased
before assignment.
c=a++;
cout<<”Line 1-Value of a++
is;”<<c<<endl;

//After expression value of a is


increased
cout<<”Line 2- Value of a
is:”<<a<<endl;
//Value of a will be increased before
assignment.
c = a++;
cout<<”Line 3 – Value of ++a
is:”<<c<<endl;
}

PROGRAM-7
Write a program to perform
arithmetic operations on complex
numbers using operator overloading.
#include<iostream>
using namespace std;
class Complex
{
public:
float real;
float img;
complex(){
this->real = 0.0;
this->img = 0.0;
}

Complex(float real,float img){


this->real=real;
this->img=img;
}
//overloading + operator
Complex operator+(const Complex &
obj){
Complex temp;
temp.img = this->img + obj.img;
temp.real=this->real + obj.real;
return temp;
}
//overloading – operator
Complex operator (const Complex &
obj){
complex temp;
temp.img = this->img – obj.img;
temp.real = this->real – obj.real;
return temp;
}
void display()
{
cout<<this->real<<”+”<<this-
>img<<”i”<<endl;
}
};
int main()
{
Complex a,b,c;
cout<<”Enter real and complex
coefficient of the first complex
number:”<<endl;
cin >> a.real;
cin >> a.img;
cout<<”Enter real and complex
coefficient of the second complex
number:”<<

complex statement;
void statement”;
}

PROGRAM-8
Write a program to illustrate the
example of constructor and
destructor.
include<iostream>
using namespace std;
class test
{
public:
int y,z;
test()
{
y=7;
z=13;
}
};
int main()
{
test a;
cout<<”the sum is:”<< a.y+a.z;
return 1;
}

PROGRAM-9
Write a program to find product of
two numbers entered by the user.
#include<iostream>
using namespace std;
int main()
{
float a,b,product;
cout <<”Enter two Numbers\n”;
cin >> a >> b;
product = a*b;
cout << ”Product = “<< product;
}

PROGRAM-10
Write a program to illustrate the
concept of constructor and destructor
#include<iostream>
using namespace std;

int main()
{
int a = 21;
int c;
//Value of a will not be increased
before assignment.
c = a++;
cout<<”Line 1 – Value of a++
is ;”<<c<<endl ;

//After expression value of a is


increased
cout<<”line 2 –Value of a
is :”<<a<<endl;
//Value of a will be increased before
assignment.
c = ++a;
cout<<”Line 3 – Value of ++a
is ;”<<c<<endl ;
}
Write a program to perform
arithmetic operations on complex
numbers using operator overloading.

#include<iostream>
using namespace std;
class complex
{
public:
float real;
float img;

Complex(){
this->real = 0.0;
this->img = 0.0;
}

Complex(float real, float img){


this->real = real;
this->img = img;
}

//overloading + operator
Complex temp;
temp.img = this->img + obj.img;
temp.real = this->real + obj.real;
return temp;
}
//overloading + operator
Complex operator-(const Complex &
obj){
Complex temp;
temp.img = this->img – obj.img;
temp.real = this->real – obj.real;
return temp;
}
void display()
{
cout << this->real << ”+” << this->img
<<”i” <<endl;
}
};
int main()
{
Complex a,b,c;
cout<<”Enter real and complex
coefficient of the first complex
number:”<<endl;
cin>>a.real;
cin>>a.img;

cout<<”Enter real and complex


coefficient of the second complex
number:”<<endl;
cin >> b.real;
cin >> b.img;
cout<<Enter real and complex
coefficient of the third complex
number:”<<endl;
cin >> b.real;
cin >> b.img;
}

PROGRAM-12
Write a program to find the product of
two numbers entered by the user.

#include<iostream>
using namespace std;
int main()
{
float a , b , product;
cout << “Enter two numbers\n”;
cin >> a >> b;
product = a*b;
cout <<”Product = “<< product;
}

You might also like