Question

In: Computer Science

C++: 1.When and in what order are constructors and destructors called? 2. Create a class without...

C++:

1.When and in what order are constructors and destructors called?

2. Create a class without any constructors, and show that you can create objects with the default constructor. Now create a non-default constructor (one with an argument, aka parameterized constructor) for the class, and try compiling again. Explain what happened.

Solutions

Expert Solution

Question 1:

Constructors are called in the order in which they are inherited, and for destructors it is reverse.

For example see this program:

#include <iostream>
using namespace std;

class A
{
public:
    //Making constructor of base class
    A()
    {
        cout << "Base constuctor called\n";
    }
    //Making destructor of base class
    ~A()
    {
        cout << "Base destructor called\n";
    }
};

class B : public A
{

public:
    //Making constructor of derived class
    B()
    {
        cout << "Derived constructor called\n";
    }
    //Making destructor of derived class
    ~B()
    {
        cout << "Derived destructor called\n";
    }
};

int main()
{
    B ob;
    return 0;
}

OUTPUT:

Question 2:

Here is a class without any constructors (ie default), it runs fine.

#include <iostream>
using namespace std;

//Making the class
class Box
{
    int l, b;

public:
    //Function to show area
    void showarea()
    {
        l = 10;
        b = 20;
        cout << "Area of the box is :" << l * b << "\n";
    }
};

int main()
{
    Box ob;
    ob.showarea();
    return 0;
}

Here is the output of above code:

If we make a parametried constructor then we have to give the parameters while making the object of the class, otherwise it will give error. See this code for example:

#include <iostream>
using namespace std;

class Box
{
    int l, b;

public:
    Box(int aa, int bb)
    {
        l = aa;
        b = bb;
    }
    void showarea()
    {
        cout << "Area of the box is :" << l * b << "\n";
    }
};

int main()
{
    // Box ob; //This line will cause error
    //To fix the error uncomment the below line and comment the above line
    Box ob(50, 60);
    ob.showarea();
    return 0;
}

OUTPUT:


Related Solutions

Create a class called College that has: At least 2 constructors (one should be the default...
Create a class called College that has: At least 2 constructors (one should be the default constructor) All members private The relevant setters and getters A print_me() function A college_ID member (unique) Write two unsorted lists that manage the data for colleges and their ranking: An unsorted list using an array An unsorted list using a linked list Each college will have a unique ID number that will be used for search. For the lab you don't need to read...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class this class should contain the following private data types: - name (string) - id number (string) - email (s) - phone number (string) - number of courses (int) - a dynamic array of course objects. the user will specify how many courses are there in the array. the following are public members of the student class: - default constructor (in this constructor, prompt the...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
You are to create a class called ManageDB. The ManageDB class will have a 2 input...
You are to create a class called ManageDB. The ManageDB class will have a 2 input constructor that has the following definition: ManageDB(int number, String fileName) The constructor will create an array of EmployeeDB objects of length "number". The constructor will read the file located at "fileName" and extract the information from that file to populate a database of EmployeeDB objects. The file contains information on the EmployeeDB objects. This information is contained in records. The format of each record...
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT