0% found this document useful (0 votes)
20 views21 pages

Assignment CPP

Uploaded by

Shubhomoy Paul
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)
20 views21 pages

Assignment CPP

Uploaded by

Shubhomoy Paul
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/ 21

ASSIGNMENT-01

Calculate area of square using function

CPP Source Code

#include<iostream>
using namespace std;
class square{
public:void area(int h)
{
cout<<"Result= "<<h*h;
}
};
int main(){
square s;
int a;
cout<<"Enter the side of square= ";
cin>>a;
s.area(a);
}

Output

Discussion
Here I create a class named square. In the class I create a function named area and, in the function, I
write the code for calculating the area of the square. In the main function I take input of the side of
square and called the function, area. And these give me the output.

ASSIGNMENT-02

Calculate area of square using function declared out of the class

CPP Source Code

#include<iostream>
using namespace std;
class square{
public:void area(int);
};
void square::area(int h){
cout<<"Result= "<<h*h;
}
int main(){
square s;
int a;
cout<<"Enter the side of square= ";
cin>>a;
s.area(a);
}

Output

Discussion
Here I create a class named square. In the class I create a function named area and, outside the
function, I write the code for calculating the area of the square. In the main function I take input of the
side of square and called the function, area. And these give me the output.

ASSIGNMENT-03

Calculate area of square using constructor

CPP Source Code

#include<iostream>
using namespace std;
class square{
public:int side;
public:square()
{
side=10;
}
public:void area(int);
};
void square::area(int h){
cout<<"Area= "<<h*h;
}
int main(){
square s;
cout<<"When side of square is 10"<<"\n";
s.area(s.side);
}

Output

Discussion
Here I create a class named square. In the class I create a constructor named side and, in the
constructor I declared a variable, after that I create a function and into that I write the code for
calculating the area of the square. In the main function I take input of the side of square and called the
function, area. And these give me the output.

ASSIGNMENT-04

Calculate area and perimeter of circle, triangle, rectangle

CPP Source Code


#include<iostream>
using namespace std;
class circle
{
public:void area(float,float);
public:void perimeter(float,float);
float pi;
circle()
{
pi=3.14;
}
};
class triangle
{
public:void area(float,float);
public:void perimeter(int,int,int);
};
class rectangle
{
public:void area(int,int);
public:void perimeter(int,int);
};
void circle::area(float pi,float r)
{
cout<<"Area of circle= "<<pi*r*r;
}
void circle::perimeter(float pi,float r)
{
cout<<"Perimeter of circle: "<<2*pi*r;
}
void triangle::area(float h,float b)
{
cout<<"Area of triangle: "<<0.5*h*b;
}
void triangle::perimeter(int a,int b,int c)
{
cout<<"Perimeter of triangle: "<<a+b+c;
}
void rectangle::area(int p,int q){
cout<<"Area of rectangle: "<<p*q;
}
void rectangle::perimeter(int p,int q){
cout<<"Perimeter of rectangle: "<<2*(p+q);
}
int main(){
int a,b,c,s,h,p,q;float r,hi,ba;
do{
cout<<"\n1. Area of circle\n";
cout<<"2. Perimeter of circle\n";
cout<<"3. Area of triangle\n";
cout<<"4. Perimeter of triangle\n";
cout<<"5. Area of rectangle\n";
cout<<"6. Perimeter of rectangle\n";
circle C;
triangle T;
rectangle R;
cout<<"Enter your choice: ";
cin>>s;
switch(s)
{
case 1:
cout<<"Enter radious: ";
cin>>r;
C.area(C.pi,r);
break;
case 2:
cout<<"Enter radious: ";
cin>>r;
C.perimeter(C.pi,r);
break;
case 3:
cout<<"Enter the hight: ";
cin>>hi;
cout<<"Enter the base: ";
cin>>ba;
T.area(hi,ba);
break;
case 4:
cout<<"Enter the 3 side: ";
cin>>a>>b>>c;
T.perimeter(a,b,c);
break;
case 5:
cout<<"Enter hight: ";
cin>>p;
cout<<"Enter width: ";
cin>>q;
R.area(p,q);
break;
case 6:
cout<<"Enter hight: ";
cin>>p;
cout<<"Enter width: ";
cin>>q;
R.perimeter(p,q);
break;
default:
cout<<"Input error";
}
}while(s!=7);
}

Output

Discussion
Here I use constructor for defining the value of pi. I use several functions for all operations. And at last
use switch-case for execute the program.
ASSIGNMENT-05

Perform addition, subtraction, multiplication, division of complex number

CPP Source Code


#include<iostream>
using namespace std;
class complex{
public:float x1,x2;
public:float y1,y2;
public:
complex(float real1,float imag1,float real2,float imag2){
x1=real1;
y1=imag1;
x2=real2;
y2=imag2;
}
public:void add();
public:void sub();
public:void mul();
public:void div();
void display();
};

void complex::add(){
cout<<"\n"<<x1+x2<<"+i"<<y1+y2;}
void complex::sub(){
cout<<"\n"<<x1-x2<<"-i"<<y1-y2;}
void complex::mul(){
cout<<"\n"<<(x1*x2)-(y1*y2)<<"+i"<<(x1*y2)+(x2*y1);}

void complex::div(){
cout<<"\n"<<((x1*x2)/((x2*x2)+(y2*y2)))+((y1*y2)/((x2*x2)+
(y2*y2)))<<"+i"<<((x2*y1)/((x2*x2)+(y2*y2)))-((x1*y2)/((x2*x2)+(y2*y2)));
}
int main(){
int x,y,a,b;
cout<<"Enter real of 1st: ";
cin>>x;
cout<<"Enter imag of 1nd: ";
cin>>y;
cout<<"Enter real of 2st: ";
cin>>a;
cout<<"Enter imag of 2nd: ";
cin>>b;
complex c1(x,a,y,b);
c1.add();
c1.sub();
c1.mul();
c1.div();
return 0;
}

Output

Discussion
Here I used parameterised constructor. Using the above algorithm, the program runs successfully.
ASSIGNMENT-06

Write a program to implement inheritance using parameterized constructor

CPP Source Code

#include<iostream>
using namespace std;
class A{
public :int l,b;
public:A(int x,int y){
l=x;
b=y;
}
};
class B:public A
{
public:
B(int x,int y) : A(x, y){
}
public : void show(){
cout<<"l = "<<l<<" and b = " << b;
}
};
int main()
{
B b = B(3,2);
b.show();
}
Output

Discussion
Here I used parameterised constructor and implemented inheritance. Using the above algorithm, the
program runs successfully.

ASSIGNMENT-07

Write a program to implement inheritance using default constructor

CPP Source Code

#include<iostream>
using namespace std;
class A{
public :int l,b;
public:A(){
l=3;
b=2;
}
};
class B:public A
{
public : void show(){
cout<<"l = "<<l<<" and b = " << b;
}
};
int main()
{
B b;
b.show();
}

Output

Discussion
Here I used default constructor and implemented inheritance. Using the above algorithm, the program
runs successfully.

ASSIGNMENT-08

Calculate area and perimeter of circle, rectangle and triangle using inheritance
CPP Source Code
#include<iostream>
#include<iostream>
using namespace std;
class shape
{
public:double a;
public:double b;
public:double r;
public:double pi;
public:shape(){
pi=3.14;}
};
class circle:public shape{
public:void cal1(float p){
cout<<"Enter radius of circle: \n";
cin>>r;
cout<<"\n Perimeter= "<<2*p*r;
cout<<"\n Area= "<<p*r*r<<"\n";
}
};
class rectangle:public shape{
public:void cal2(){
cout<<"Enter height and width of rectangle: \n";
cin>>a>>b;
cout<<"\n Perimeter= "<<2*(a+b);
cout<<"\n Area= "<<a*b<<"\n";
}
};
class triangle:public shape{
public:void cal3(){
cout<<"Enter 3 side of triangle: \n";
cin>>a>>b>>r;
cout<<"\n Perimeter= "<<a+b+r;
cout<<"\n Enter height and base of triangle: \n";
cin>>a>>b;
cout<<"\n Area= "<<0.5*b*a<<"\n";
}
};
int main(){
shape s;
circle c;
rectangle rec;
triangle tri;
c.cal1(c.pi);
rec.cal2();
tri.cal3();
}
Output

Discussion
Here I used inheritance. ‘shape’ is the master class and ‘circle’, ‘rectangle’,
‘triangle’ is the sub class. Using the above algorithm, the program runs
successfully.
ASSIGNMENT-09

Write a program on Operator (==) Overloading

CPP Source Code

#include<iostream>
#include<string.h>
using namespace std;
class String{
private:char str[100];
public:
String(){
strcpy(str," ");
}
String(char *s){
strcpy(str,s);
}
void display(){
cout<<str;
}
void getstr(){
cin.get(str,50);
}
bool operator == (String ss){
return((strcmp(str,ss.str)==0)?true:false);
}
};
int main()
{
String s1="world";
String s2;
cout<<"\nEnter string=";
s2.getstr();
cout<<"\n s1 :";
s1.display();
cout<<"\n s2 :";
s2.display();
if(s2==s1)
cout<<"\ntwo strings match";
else
cout<<"\ntwo strings dont match";
return 0;
}

Output

Discussion
Here I used operator overloading. Using the above algorithm, the program runs
successfully.
ASSIGNMENT-10

Write a program on Operator (+) Overloading

CPP Source Code


#include<iostream>
#include<string.h>
using namespace std;

class String{
private:char str[100];
public:
String(){
strcpy(str," ");
}
String(char *s){
strcpy(str,s);
}
void display(){
cout<<str;
}
void getstr(){
cin.get(str,50);
}
String operator + (String ss){
return(strcat(str,ss.str));
}
};
int main()
{
String s1="wel";
String s2;
cout<<"\nEnter string to concatinate = ";
s2.getstr();
cout<<"\n string 1 :";
s1.display();
cout<<"\n string 2 :";
s2.display();
String r=s1+s2;
cout<<"\n Concatinated String : ";
r.display();
return 0;
}

Output

Discussion
Here I used operator overloading. Using the above algorithm, the program runs
successfully.
ASSIGNMENT-11

Write a program on friend() function

CPP Source Code

#include<iostream>
#include<string.h>
using namespace std;
class friendfunc{
private : int meter;
public : friendfunc(){
meter = 0;}
friend int addFive(friendfunc);

};}
int addFive(friendfunc d){
d.meter+= 5;
return d.meter;}
int main(){
friendfunc d;
cout<<"Answer = "<<addFive(d);
return 0;
}

Output
Discussion
Here I used the friend function to understand its concept. Using the above
algorithm, the program runs successfully.

ASSIGNMENT-12

Write a program on template to find the largest among double, char, float, int.

CPP Source Code

#include<iostream>
#include<string.h>
using namespace std;
template<class s>
s larger(s n1,s n2){
if (n1>n2)
return n1;
else
return n2;
}
int main()
{
int i1,i2;double d1,d2;
float f1,f2;char c1,c2;
cout<<"\nenter 2 integers : ";
cin>>i1>>i2;
cout<<"\nLargest : "<<larger(i1,i2);
cout<<"\nenter 2 double : ";
cin>>d1>>d2;
cout<<"\nLargest : "<<larger(d1,d2);
cout<<"\nenter 2 float : ";
cin>>f1>>f2;
cout<<"\nLargest : "<<larger(f1,f2);
cout<<"\nenter 2 char : ";
cin>>c1>>c2;
cout<<"\nLargest : "<<larger(c1,c2);
}
Output

Discussion
Here I used the template to understand its concept. Using the above algorithm, the
program runs successfully.
ASSIGNMENT-13

Write a program on Exception Handling.

CPP Source Code


#include<iostream>
#include<string.h>
using namespace std;
int division (int a ,int b){
if(b==0)
throw "cannot be divided by 0 ";
else
return(a/b);
}
int main()
{
try{
int x,y,z=0;
cout<<"enter number : ";
cin>>x>>y;
z=division(x,y);
cout<<"qoutient : "<<z;
}
catch(const char* msg){
cout<<msg;
}
}
Output
Discussion
Here I used the exception handling to understand its concept. Using the above
algorithm, the program runs successfully.

You might also like