C++: Classes And Objects

Question:17
C++ program to calculate and print area and perimeter of the square?
Sol:

#include <iostream>
#include<conio.h>
using namespace std;
class square
{
private:
    int s,a,p;
public:
    void read();
    void cal();
    void show();
};
void square::read()
{
    cout<<"Enter side of square ";
    cin>>s;
}
void square::cal()
{
    a=s*s;
    p=4*s;
}
void square::show()
{
    cal();
    cout<<"Area = "<<a<<endl;
    cout<<"Perimeter  = "<<p<<endl;
}
int main()
{
    square s;
    s.read();
    s.show();
    return(0);
}
/* Output */

Enter side of square 5
Area = 25
Perimeter  = 20

Question:18
C++ program to calculate and print area and perimeter of rectangle?
Sol:

#include <iostream>
#include<conio.h>
using namespace std;
class rectangle
{
private:
    int l,b,a,p;
public:
    void read();
    void cal();
    void show();
};
void rectangle::read()
{
    cout<<"Enter length and width of rectangle  ";
    cin>>l>>b;
}
void rectangle::cal()
{
    a=l*b;
    p=2*(l+b);
}
void rectangle::show()
{
    cal();
    cout<<"Area = "<<a<<endl;
    cout<<"Perimeter  = "<<p<<endl;
}
int main()
{
    rectangle r;
    r.read();
    r.show();
    return(0);
}
/* Output */

Enter length and width of rectangle  5
6
Area = 30
Perimeter  = 22

Question:19
C++ program to calculate and print area and circumference of a circle?
Sol:

#include <iostream>
#include<conio.h>
using namespace std;
class circle
{
private:
    float r,a,c;
public:
    void read();
    void cal();
    void show();
};
void circle::read()
{
    cout<<"Enter radius of circle  ";
    cin>>r;
}
void circle::cal()
{
    a=3.1415*r*r;
    c=2*3.1415*r;
}
void circle::show()
{
    cal();
    cout<<"Area = "<<a<<endl;
    cout<<"Circumference  = "<<c<<endl;
}
int main()
{
    circle r;
    r.read();
    r.show();
    return(0);
}
/* Output */

Enter radius of circle  2.3
Area = 16.6185
Circumference  = 14.4509

Question:20
C++ program to calculate and print Simple Interest?
Sol:

#include <iostream>
#include<conio.h>
using namespace std;
class interest
{
private:
    float p,r,t,si;
public:
    void read();
    void cal();
    void show();
};
void interest::read()
{
    cout<<"Enter p,r and t  ";
    cin>>p>>r>>t;
}
void interest::cal()
{
    si=(p*r*t)/100;
}
void interest::show()
{
    cal();
    cout<<"Simple Interest  = "<<si<<endl;
}
int main()
{
    interest r;
    r.read();
    r.show();
    return(0);
}
/* Output */

Enter p,r and t  1000
5
6
Simple Interest  = 300