logo codesdope
Level 1
1.
Create a class with a function that prints "This is parent class" and its subclass with another function that prints "This is child class". Now, create an object for each class and call
1 - function of the parent class by the object of the parent class
2 - function of the child class by the object of the child class
3 - function of the parent class by the object of the child class
Answer
#include <iostream>
using namespace std;

class Parent
{
public:
    Parent()
    {}
    void parent_print()
    {
        cout << "This is parent class" << endl;
    }
};

class Child: public Parent
{
public:
    void child_print()
    {
        cout << "This is child class" << endl;
    }
};

int main()
{
    Parent p;
    Child c;
    p.parent_print();
    c.child_print();
    c.parent_print();
    return 0;
}                                  

2.
Create a class named 'Member' having the following members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
It also has a function named 'printSalary' which prints the salary of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and 'Manager' classes have data members 'specialization' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a manager by making an object of both of these classes and print the same.

3.
Create a class named 'Rectangle' with two data members 'length' and 'breadth' and two functions to print the area and perimeter of the rectangle respectively. Its constructor having parameters for length and breadth is used to initialize the length and breadth of the rectangle. Let class 'Square' inherit the 'Rectangle' class with its constructor having a parameter for its side (suppose s) calling the constructor of its parent class. Print the area and perimeter of a rectangle and a square.
Answer
#include <iostream>
using namespace std;

class Rectangle
{
    int length,breadth;
public:
    Rectangle(int l, int b)
    {
        length = l;
        breadth = b;
    }

    void print_area()
    {
        cout << length*breadth << endl;
    }

    void print_perimeter()
    {
        cout << 2*(length+breadth) << endl;
    }
};

class Square : public Rectangle
{
public:
    Square(int side) : Rectangle(side,side)
    {}
};

int main()
{
    Rectangle r(4,5);
    Square s(4);
    r.print_area();
    r.print_perimeter();
    s.print_area();
    s.print_perimeter();
    return 0;
}                                  

4.
Now repeat the above example to print the area of 10 squares.
Hint-Use array of objects

5.
Create a class named 'Shape' with a function to print "This is a shape". Then create two other classes named 'Rectangle' and 'Circle' inheriting the Shape class, both having a function to print "This is rectangular shape" and "This is circular shape" respectively. Create a subclass 'Square' of 'Rectangle' having a function to print "Square is a rectangle". Now call the function of the 'Shape' and the 'Rectangle' class by the object of the 'Square' class.

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.