Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ programming questions, most points to most complete! Question 8.8. If you ha

ID: 3771780 • Letter: C

Question

c++ programming questions, most points to most complete!

Question 8.8. If you have the following class definitions, which of the following is the proper way to construct an object of the derived class?

class Pet
{
public:
Pet();
void printPet();
string getName();
void setName(string newName);
private:
string name;
};

class Dog:public Pet
{
public:
Dog();
void printPet();
void setType(string newType);
string getType();
private:
string type;
}; (Points : 2)


Dog::Dog():Pet(),type("MUTT")
{
}


Dog::Dog()
{
name="Rover";
}


Pet::Dog():Pet(),type("MUTT")
{
}


Dog::Pet():Pet(),type("MUTT")
{
}

Question 9.9. If a base class has public member functions that are not listed by a derived class, then these functions (Points : 2)
are not available to the derived class
are inherited unchanged in the derived class
are private to the derived class
do not exist in the derived class

Question 10.10. If you have a copy constructor in the base class, but do not have a copy constructor for the derived class, then (Points : 2)
you will have a syntax error
a copy constructor for the derived class is automatically created for you
you can not use pointer variables
the default constructor is used

Question 11.11. Which of the following would correctly call the base class (BaseClass) assignment operator from the derived class (DerivedClass) assignment operator? (Points : 2)
leftSide=rightSide;
rightSide=BaseClass.rightSide;
BaseClass::operator=(rightSide);
DerivedClass::rightSide=BaseClass::rightSide;

Question 12.12. Given a class A that derives from a class B that derives from a class C, when an object of class A goes out of scope, in which order are the destructors called? (Points : 2)
C, B, then A
A, B, then C
unable to determine
depends on how the code is written for the destructors

Question 13.13. Given the following classes and code, what is the output of the last statement shown?

class Pet
{
public:
virtual void print();
string name;
private:
};
class Dog: public Pet
{
public:
void print();
string breed;
};
void Pet::print()
{
cout << "My name is " << name;
}
void Dog::print()
{
Pet::print();
cout << ", and my breed is a "<< breed << endl;
}

Pet* pPtr;
Dog* dPtr=new Dog;
dPtr->name= "Rover";
dPtr->breed="Weiner";
pPtr= dPtr;
pPtr->print(); (Points : 2)
My name is Rover
My name is Rover, and my breed is a Weiner
, and my breed is a Weiner
nothing

Question 14.14. Which of the following should be virtual if a base class uses dynamic memory allocation? (Points : 2)
the constructor
the copy constructor
the print function
the destructor

Question 15.15. If a base class has a non-virtual member function named print, and a pointer variable of that class is pointing to a derived object, then the code ptr->print( ); calls (Points : 2)
the base class print function
the derived print function
both the derived and base print functions
it causes a run-time error

Question 16.16. What is the output of the following code fragment?

int f1(int base, int limit)
{
if(base > limit)
return -1;
else
if(base == limit)
return 1;
else
return base * f1(base+2, limit);
}

int main()
{
cout << f1(2,4)<<endl;
return 0;
} (Points : 2)
-1
2
1
0
It depends

Explanation / Answer

Here by I am giving answers.

Question 8.8. If you have the following class definitions, which of the following is the proper way to construct an object of the derived class?

class Pet
{
public:
Pet();
void printPet();
string getName();
void setName(string newName);
private:
string name;
};

The following declaration as given by you is correct the other 3 are wrong.

class Dog:public Pet
{
public:
Dog();
void printPet();
void setType(string newType);
string getType();
private:
string type;
};


Question 9.9. If a base class has public member functions that are not listed by a derived class, then these functions (Points : 2)
are not available to the derived class
Answer:

are private to the derived class
Question 10.10. If you have a copy constructor in the base class, but do not have a copy constructor for the derived class, then (Points : 2)
Answer:
the default constructor is used
Question 11.11. Which of the following would correctly
call the base class (BaseClass) assignment operator from
the derived class (DerivedClass) assignment operator? (Points : 2)
Answer:
DerivedClass::rightSide=BaseClass::rightSide;
Question 12.12. Given a class A that derives from a class B that derives from
a class C, when an object of class A goes out of scope, in which order are
the destructors called? (Points : 2)
Answer:
C, B, then A


Question 13.13. Given the following classes and code, what is the output of the last statement shown?
class Pet
{
public:
virtual void print();
string name;
private:
};
class Dog: public Pet
{
public:
void print();
string breed;
};
void Pet::print()
{
cout << "My name is " << name;
}
void Dog::print()
{
Pet::print();
cout << ", and my breed is a "<< breed << endl;
}
Pet* pPtr;
Dog* dPtr=new Dog;
dPtr->name= "Rover";
dPtr->breed="Weiner";
pPtr= dPtr;
pPtr->print(); (Points : 2)
Answer:
nothing


Question 14.14. Which of the following should be virtual if a base class
uses dynamic memory allocation? (Points : 2)
Answer:
the copy constructor
Question 15.15. If a base class has a non-virtual member function named print,
and a pointer variable of that class is pointing to a derived object,
then the code ptr->print( ); calls (Points : 2)
Answer:
the base class print function

Question 16.16. What is the output of the following code fragment?
int f1(int base, int limit)
{
if(base > limit)
return -1;
else
if(base == limit)
return 1;
else
return base * f1(base+2, limit);
}
int main()
{
cout << f1(2,4)<<endl;
return 0;
} (Points : 2)
Answer:
2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote