Assignment CPP
Assignment CPP
#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
#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
#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
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
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
#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
#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
#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
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
#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.
#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