C++ Program 1
C++ Program 1
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;
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 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 ;
#include<iostream>
using namespace std;
class complex
{
public:
float real;
float img;
Complex(){
this->real = 0.0;
this->img = 0.0;
}
//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;
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;
}