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 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 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...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT