Skip to content

Instantly share code, notes, and snippets.

@ashwinkey04
Last active August 20, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwinkey04/794d7043159bf0518189173e12ad1481 to your computer and use it in GitHub Desktop.
Save ashwinkey04/794d7043159bf0518189173e12ad1481 to your computer and use it in GitHub Desktop.
Adv Prog Lab - 1

Lab - 1

Ashwin R AM.EN.U4CSE19343

a. Make the following attributes private and make modifications to the program

double length;

double breadth;

double height;

Answer

#include <iostream>
using namespace std;

class Room
{
private:
    double length;
    double breadth;
    double height;

public:
    void set_length(double val)
    {
        length = val;
    }

    void set_breadth(double val)
    {
        breadth = val;
    }

    void set_height(double val)
    {
        height = val;
    }

    double calculateArea()
    {
        return length * breadth;
    }
    double calculateVolume()
    {
        return length * breadth * height;
    }
};
int main()
{
    // create object of Room class
    Room room1;

    // assign values to data members
    room1.set_length (42.5);
    room1.set_breadth ( 30.8);
    room1.set_height (19.2);
    // calculate and display the area and volume of the room
    cout << "Area of Room = " << room1.calculateArea() << endl;
    cout << "Volume of Room = " << room1.calculateVolume() << endl;
    return 0;
}

Output

Screenshot from 2020-08-20 15-23-48


b. Add constructor to following functions. See the sample code and make modifications.

  • Create a default constructor

  • Create a parameterized constructor.

  • Create an object in driver class which takes the following values.

    • double length = 10.8;
    • double breadth = 8.6;
    • double height = 15.5;
  • Create a copy constructor. This takes the object created in parameterized constructor as in input.

Answer

#include <iostream>
using namespace std;

class Room
{
private:
    double length;
    double breadth;
    double height;

public:
    Room()//Default
    {
        cout << "Default constructor called\n";
    }
    Room(double l, double b, double h)//Parameterized
    {
        cout << "Paramterized constructor called\n";
        length = l;
        breadth = b;
        height = h;
    }
    Room(const Room &r1){
        cout << "Copy constructor called\n";
        length = r1.length;
        breadth = r1.breadth;
        height = r1.height;
    }
    void set_length(double val)
    {
        length = val;
    }

    void set_breadth(double val)
    {
        breadth = val;
    }

    void set_height(double val)
    {
        height = val;
    }

    double calculateArea()
    {
        return length * breadth;
    }
    double calculateVolume()
    {
        return length * breadth * height;
    }
};
int main()
{
    // create object of Room class
    Room room1;// Calls default constructor
    Room room2(10.8,8.6,15.5); //Calls parameterized constructor
    Room room3(room2);//Calls copy constructor

    // calculate and display the area and volume of the room
    cout << "Area of Room = " << room3.calculateArea() << endl;
    cout << "Volume of Room = " << room3.calculateVolume() << endl;
    return 0;
}

Output

Screenshot from 2020-08-20 15-25-35


1. Create a class circle with the following data members and member functions. Create the following instances of circle and display their values assigned to the data members.

image

Answer

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

class Circle
{
private:
    double radius;
    string color;

public:

    Circle()//Default
    {
        radius = 1.0;
        color = "red";
    }

    Circle(double r)//Parameterized
    {
        radius = r;
        color = "red";
    }

    Circle(double r, string c)//Parameterized
    {
        radius = r;
        color = c;
    }
    double getRadius(){
        return radius;
    }
    string getColor(){
        return color;
    }
    double getArea(){
        return 3.14*(radius*radius);
    }
};
int main()
{
    Circle c1(2.0,"blue");  // Calls parameterized constructor
    Circle c2(2.0);     //Calls parameterized constructor
    Circle c3;  //Calls default constructor
    
    //C1 Instances    
    cout << "Radius of Circle = " << c1.getRadius() << endl;
    cout << "Color of Circle = "<< c1.getColor() << endl;
    cout << "Area of Circle = " << c1.getArea() << endl << endl;

    //C2 Instances    
    cout << "Radius of Circle = " << c2.getRadius() << endl;
    cout << "Color of Circle = "<< c2.getColor() << endl;
    cout << "Area of Circle = " << c2.getArea() << endl << endl;

    //C3 Instances    
    cout << "Radius of Circle = " << c3.getRadius() << endl;
    cout << "Color of Circle = "<< c3.getColor() << endl;
    cout << "Area of Circle = " << c3.getArea() << endl << endl;
    return 0;
}

Output

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment