Question

In: Computer Science

I. Given the following code segment below what is the best description of line 5 and...

I. Given the following code segment below what is the best description of line 5 and line 6? Identify the public and private members.

1. #include <iostream.h>

2. class SimpleCat

3. {

4. public:

5. SimpleCat (int age, int weight);

6. ~SimpleCat(){}

7. int GetAge() {return itsAge;}

8. int GetWeight() {return itsWeight;}

9. private:

10. int itsAge;

11. int itsWeight;

12. };

II. Multiple Choice questions

1. A function that is called automatically each time an object is created or

instantiated is

a. constructor b. Destructor c. Copy constructor


2. A constructor may be _____________.

a. provided by you b. overloaded c. both a and b


3. A class named Gymnast must have this destructor.

a. Gymnast b. Destructor c. ~Gymnast d. *destructor


4. The return type for all constructors is this.

a. void b. int c. float d. double e. no type is allowed



5. A class named Building has a method getfloors( ). If School is a child class of Building and AMA is an object of type School then which of the following are valid?

a)Building.getfloors(); b)School.getfloors( ) c)AMA.getfloors();

III. What is the output ?

class A

{

int a,b,c;

public:

A( )

{

cout<<"A"<<endl;

}

~A( )

{

cout<<"B"<<endl;

}

void add( )

{

a=2, b=3;

c=a+b;

cout<<c<<endl;

}

};

void main()

{

A ab;

ab.add( );

}

Solutions

Expert Solution

1.)

  • Line 5 containes SimpleCat(int age, int weight). This is a parametrized constructor for the class SimpleCat
  • Line 6 contains ~SimpleCat(). This is a destructor for the class SimpeCat
  • Public members of the class are declared under the public: access specifier. They are
    • GetAge()
    • GetWeight()
  • Private members are specified under private: access specifier. They are:
    • itsAge
    • itsWeight

2.)

  • Constructors are called each time an objects is created. they are used to initalize values for data members
  • Answer: Constructors

3.)

  • A constructor may be provided by us, that is it can be defined by us.
  • A constructor can be overloaded by providing different number of parameters to it.
  • Answer: both a and b

4.)

  • The desctructor of a class has the following syntax: ~<class-name>
  • Answer: ~Gymnast

5.)

  • A constuctor cannot return any value. It must have no return type under any case.
  • Answer: no type is allowed

6.)

  • If school inherits from Building, then every public methods of Building will be inhertied into School
  • Also, these members cannot be accessed using classes, it must be accessed using Objects. Here AMA is an object
  • Answer: AMA.getFloors()

7.)

  • class A has a constructor that print 'A'. So, when the object ab is created, then this character will be printed along with a newline
  • add() adds 2 and 3, and prints the result 5, along with a newline.
  • When program terminates, the destructor of A is called. The destructor in class A prints "B", along with a newline
  • Answer:

A

5

B


Related Solutions

What is the Θ( ) for each code segment below? (a) for( int i = 1,...
What is the Θ( ) for each code segment below? (a) for( int i = 1, i<= n, i = i*2){       some constant operation } (b) for( int i = 1, i<= n, i++){      for( int j = 2*i, j<=n, j++){           some constant operation      } } (c) for( int i = 1, i<= n, i = i*2){      for( int j = 1, j<=n, j = j*2){           some constant operation      } }
Show what is written by the following segment of code, given item1, item2, and item3 are...
Show what is written by the following segment of code, given item1, item2, and item3 are int variable: item1 = 4; item3= 0; item2 = item1 + 1; stack.Push(item2); stack.Push(item2 +1); stack.Push(item1); item2 = stack.Top(); stack.Pop(); item1 = item2 + 1; stack.Push(item1); Stack.Push(item3); while (!stack.IsEmpty()) {      item3 = stack.Top():      cout <<item3<<endl;      stack.Pop(); } cout<<item1 <<endl <<item2<<endl<<item3<<endl;
Given the following code segment, tell the output if it is part of an executable program....
Given the following code segment, tell the output if it is part of an executable program. Assume the addresses of the variables, a and b, are 0012A32A and 0012A340, respectively.        int* intPtr1, *intPtr2;        int a = 10, b;        intPtr1 = &a;        cout<< "The data in a is "<<a<<endl;        cout << "The address of a is "<<&a<<endl;        cout <<"The data in intPtr1 is "<< intPtr1 <<endl;        cout <<"The data in the variable pointed by...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Indicate the name and one letter code for the (proteinogenic) amino acid best fitting the given description
Indicate the name and one letter code for the (proteinogenic) amino acid best fitting the given description Of the amino acids with an amide functional group in its sidechain, the one with the shortest sidechain   The amino acid that has an amino group in its sidechain;  in some proteins, it is post-translationally hydroxylated via a Vitamin C dependent enzyme The amino acid with an aromatic (but not heterocyclic) sidechain that is nonetheless capable of hydrogen bonding and is often found...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
Given the below code, compute the values of the best case, worst case and average case...
Given the below code, compute the values of the best case, worst case and average case knowing that the basic operation in the first loop takes 4 ns to execute and the basic operation in the second loop takes 5 ns to execute. int A[20]; for (int i=0; i < 20; i++) {           cin >> A[i]; } for (i=0; i < 20; i++) {           if (A[i] % 3 == 0)                     break; }
Given a segment, construct its perpendicular bisector. Given a line an a point, construct the perpendicular...
Given a segment, construct its perpendicular bisector. Given a line an a point, construct the perpendicular to the line through the point. Given a line an a point not on the line, construct the parallel to the line through the point.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT