Some important programs part 4
1. To
demonstrate the concept of function overloading applied to the member
functions.
#include<iostream>
using namespace std;
#include<stdlib.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int);
void area(float,int,int); //rectangle void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
int main()
{
int ch;
int a,b,r;
fn obj;
cout<<"\n\t\tFunction Overloading in Polymorphism";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n";
cout<<"\nEnter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radius of the Circle:"; cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
return 0;
}
2. To implement the use of Sum of Feet and Inches
passing and returning object
#include<iostream>
using namespace std;
class length
{
private:
int feet,inches;
public:
void get(int f,int i)
{
feet=f;
inches=i;
}
void putdata()
{
cout<<"Feet="<<feet;
cout<<"\tInches="<<inches;
}
friend length sum(length,length);
};
length sum(length a,length b)
{
length l;
l.feet=a.feet+b.feet;
l.inches=a.inches+b.inches;
return l;
}
int main()
{
length l1,l2,s;
l1.get(5,6);
cout<<"\nFirst value of feet and inches:\n";
l1.putdata();
l2.get(6,2);
cout<<"\nSecond value of feet and inches:\n";
l2.putdata();
s=sum(l1,l2);
cout<<"\nSum of values:\n";
s.putdata();
return 0;
}
3. To
demonstrate the use of constructor with its types and destructor
3a: To implement the use of Constructor
#include <iostream>
using namespace std;
class Wall {
private:
double length;
public:
Wall()
{
length = 6.8;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main()
{
Wall wall1;
return 0;
}
3b: To implement the use of destructors
using namespace std;
class
Line
{
public:
void setLength( double len );
public:
void setLength( double len );
double getLength( void
);
Line();
~Line();
private:
double length;
};
Line::Line(void) {
cout << "Object is being created" << endl;
}
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) { length = len;
}
double Line::getLength( void ) { return length;
}
int main()
~Line();
private:
double length;
};
Line::Line(void) {
cout << "Object is being created" << endl;
}
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) { length = len;
}
double Line::getLength( void ) { return length;
}
int main()
{
Line line;
line.setLength(6.8);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
line.setLength(6.8);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
4.Illustrate the use of static data member and static
member function
#include<iostream>
using namespace std;
class stat
{
int code;
static int count;
public:
stat()
{
code=++count;
}
void showcode()
{
cout<<"\n\tObject number is :"<<code;
}
static void showcount()
{
cout<<"\n\tCount Objects :"<<count;
}
};
int stat::count;
int main()
{
stat obj1,obj2;
obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
return 0;
}
5. To implement the use of array of objects
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main()
{
MyClass obs[4];
int i;
for(i=0; i < 4; i++)
obs[i].setX(i);
for(i=0; i < 4; i++)
cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";
return 0;
}
Note: The programs given here are pre compiled and pre runned. The remaining parts of the program will be in the next part
1 Comments
The programs whatever you have been provided here are really correct without any errors during the compilation time.
ReplyDeletethank you