Question

In: Computer Science

1. In c++, Class D is derived from class B. The class D does not contain...

1. In c++, Class D is derived from class B. The class D does not contain any data members of its own . Does the class D require constructor? If yes, why? Explain with the help of a code example.

2. State true or false, giving proper reasons[3,5]
(a) Virtual functions are used to create pointer to base class.
(b) A pointer to base class cannot be made to point to objects of derived class.
(c) Defining a derived class requires some changes in base class.
(d) Inheritance helps in making a general class into a more specific class.

3. When do we make a virtual function pure? What are the implications of making a pure virtual function?

Solutions

Expert Solution

Answer 1 -  Yes class D requires constructor even if it doesn’t contain any data member of its own. This is beacuase Base class constructor is always called from the base class constructor to provide the values to the base class constructor. Example;-

#include <iostream>
using namespace std;
class alpha{
  int p;
  public:
  alpha(int i)
  {
    p=i;
    cout<<"alpha initialized"<<endl;
  }
  void show_p(void){
    cout<<"p="<<p<<endl;}
};
class beta{
  float q;
  public:
  beta(float j){
  q=j;
  cout<<"beta initialized"<<endl;
  }
  void show_q(void){
  cout<<"q="<<q<<endl;
  }
};
class gamma: public beta , public alpha{
  int x,y;
  public:
  gamma(int a, float b, int c, int d):
  alpha (a),beta(b){
  x =c;
  y =d;
  cout<<"gamma initialized"<<endl;
  }
  void show_xy(void)
  {
  cout<<"x="<<x<<endl;
  cout<<"y="<<y<<endl;
  }
};
int main( ){
gamma g(4,10,50,20);
cout<<"\n";
g.show_p( );
g.show_q( );
g.show_xy( );
}

Answer 2 -

2(a) - True : Virtual functions are used to create a list of base class pointers and call methods of any of the derived classes without even knowing the kind of derived class object

2(b) - False : A derived class includes everything that is in the Base class and thus Base class can access all those fields and methods in Base class which are common to Base class and derived class

2(c) - False : Derived class only use or inherits the properties(fields and methods) from the Base class, base class doesn't require any changes.

2(d) - True : Base class provides the generic definations to the methods or simply they provide their declaration, with the help of inheritance Derived/child classes provides definations to the base class methods according to their functionality

Answer 3 : A pure virtual function is useful when we have a function that we want to put in the base class, but only the derived classes know what it should return. A pure virtual function makes it so the base class can not be instantiated, and the derived classes are forced to define these functions before they can be instantiated. This helps ensure the derived classes do not forget to redefine functions that the base class was expecting them to.


Related Solutions

If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
In C++ Design an Essay class that is derived from the GradedActivity class: class GradedActivity{ private:...
In C++ Design an Essay class that is derived from the GradedActivity class: class GradedActivity{ private: double score; public: GradedActivity() {score = 0.0;} GradedActivity(double s) {score = s;} void setScore(double s) {score = s;} double getScore() const {return score;} char getLetterGrade() const; }; char GradedActivity::getLetterGrade() const{ char letterGrade; if (score > 89) { letterGrade = 'A'; } else if (score > 79) { letterGrade = 'B'; } else if (score > 69) { letterGrade = 'C'; } else if (score...
How many significant figures does each measurement contain? a) 9457 b)20,000,000,000 c) 0.00007 d) 0.012345
How many significant figures does each measurement contain? a) 9457 b)20,000,000,000 c) 0.00007 d) 0.012345
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive....
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator−−():void +determineRouteStatistics()=0:void The class variables are as follows: • map: A 2D array of chars, it will represent the...
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive....
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reect that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will represent the map...
1.            Determine whether the function f from { a, b, c, d } to {a,...
1.            Determine whether the function f from { a, b, c, d } to {a, b, c, d, e} is injective (one-to-one), surjective (onto) and/or bijective (one-to- one correspondence) : f(a) = a,            f(b) = c,            f(c) = b, f(d) = e a. Is this function injective?              . surjective?              . bijective?              . If your answer is no for any of the above, explain:             b. Is there an inverse for this function?              . c. Is the composition f...
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d)....
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d). Godspeed, and good luck!!! CC11 Cookie Creations Natalie and her friend Curtis Lesperance decide that they can benefit from joining Cookie Creations and Curtis’s coffee shop. In the first part of this problem, they come to you with questions about setting up a corporation for their new business. In the second part of the problem, they want your help in preparing financial information following...
3. Let A = D + 1, B = D − 3, C = D +...
3. Let A = D + 1, B = D − 3, C = D + x, where D = dx. Calculate the differential operators AB, BC, CA and their effect on y(x) = e^3x
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what proportion of the progeny will phenotypically resemble the first parent? b) what proportion of the progeny will genotypically resemble neither parent?
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B...
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B a B A -> a A | B a C | a a a B -> b B b | a | D C -> C A | A C D -> ε
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT