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

C++ please answer the whole thing 1. Suppose vehicle is a class that defines the

ID: 3871297 • Letter: C

Question

C++ please answer the whole thing

1. Suppose vehicle is a class that defines the basic properties of a vehicle. Draw a class hierarchy in which several classes are derived from the class vehicle, and then other classes are derived from the classes derived from the class vehicle.

2. Consider the following statements:

In this declaration which class is the base class and which class is the derived class?

What is the type of this inheritance?

3. Consider the following statements:

In this declaration, which class is the base class and which class is the derived class?

What is the type of this inheritance?

4. Explain the difference between overriding and overloading a member function of a base class in a derived class.

5. Consider the following class definitions:

Which private members, if any, of smart are public members if superSmart?

Which members, functions, and/or data of the class smart are directly accessible in class superSmart

6. Explain how the members of a base class are inherited by a derived class using:

private inheritance

protected inheritance

public inheritance

7. Explain the difference between:

The private and protected members of a class.

The protected and public members of a class.

Explanation / Answer

1. Suppose vehicle is a class that defines the basic properties of a vehicle. Draw a class hierarchy in which several classes are derived from the class vehicle, and then other classes are derived from the classes derived from the class vehicle.
ANSWER :
Take an example that this program has to deal with motor vehicles, including cars, trucks, and motorcycles. The program use a class named Vehicle to represent all types of vehicles. Since cars, trucks, and motorcycles are types of vehicles, they would be represented by subclasses of the Vehicle class, as shown in below

The vechile is the class here which includes instance variables owner of vechile and vechile registration number and instance method transfervechileownership(). Vechile having three subclass Car, Truck, Motorcycle. These three uses specific type of variables and methods. Suppose class Car add an instance variables color, Truck class have average and Motorcycle have variable Name of model. If we want to write this as a program it will look like as,
class Vehicle {
   int VechileregistrationNumber;
   Vechile owner;
   void transferVechileOwnership(Person newOwner) {
       . . .
   }
   . . .
}

class Car extends Vehicle {
   Char color;
   . . .
}

class Truck extends Vehicle {
   int Average;
   . . .
}

class Motorcycle extends Vehicle {
   char ModelName;
   . . .
}
Vechile is superclass of other three class. Other three classes also called as derived class of vechile class.
-----------------------------------------------------------------------------------------------
2. Consider the following statements:
class personalComputers: public computers
{
. . .
};
In this declaration which class is the base class and which class is the derived class?
What is the type of this inheritance?
ANSWER:
In the above statement
Base class is computers
Derived class is personalComputers
In this inheritance, a derived class is created from a single base class and hence this one is Single Inheritance


Public, means all the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. Hence there are chances that they might change them. So the key members must not be declared public.
------------------------------------------------------------------------------------------------
Consider the following statements:
class fruits: food
{
. . .
};
In this declaration, which class is the base class and which class is the derived class?
What is the type of this inheritance?
ANSWER :
In the above statement
Base class is food
Derived class is fruits
In this inheritance, a derived class is created from a single base class and hence this one is also Single Inheritance
----------------------------------------------------------------------------------------------
4. Explain the difference between overriding and overloading a member function of a base class in a derived class.
ANSWER :


The above figure shows difference between overriding and overloading
To access the overridden function of the base class from the derived class, scope resolution operator :: is used. For example,
If you want to access getData() function of the base class, you can use the following statement in the derived class.
Base::getData();

-------------------------------------------------------------------------------------------------------------------
5. Consider the following class definitions:
class smart
{
public:
     void print() const;
     void set(int, int);
     int sum();
     smart();
     smart(int, int);

private:
     int x;
     int y;
     int secret();
};
class superSmart: public smart
{
public:
     void print() const;
     void set(int, int, int);
     int manipulate();
     superSmart();
     superSmart(int, int, int);
private:
     int z;
};
Which private members, if any, of smart are public members if superSmart?
Which members, functions, and/or data of the class smart are directly accessible in class superSmart
ANSWER :
Using friend function we can access private member
---------------------------------------------------------------------------------------------------------------
Explain how the members of a base class are inherited by a derived class using:
private inheritance
protected inheritance
public inheritance
ANSWER :

You can declare a derived class from a base class with different access control, i.e., public inheritance, protected inheritance or private inheritance.
#include <iostream>
using namespace std;
class base
{
.... ... ...
};
class derived : access_specifier base
{
.... ... ....
};

Example of public, protected and private inheritance in C++ is as follows;
class base
{
   public:
       int x;
   protected:
       int y;
   private:
       int z;
};

class publicDerived: public base
{
   // x is public
   // y is protected
   // z is not accessible from publicDerived
};

class protectedDerived: protected base
{
   // x is protected
   // y is protected
   // z is not accessible from protectedDerived
};

class privateDerived: private base
{
   // x is private
   // y is private
   // z is not accessible from privateDerived
}
In the above example, we observe the following things as:
1. Base has three member variables: x, y and z which are public, protected and private member respectively.
2. publicDerived inherits variables x and y as public and protected. z is not inherited as it is a       private member variable of base.
3. ProtectedDerived inherits variables x and y. Both variables become protected. z is not inherited
4. If we derive a class derivedFromProtectedDerived from protectedDerived, variables x and y are also inherited to the derived class.
5. PrivateDerived inherits variables x and y. Both variables become private. z is not inherited
6. If we derive a class derivedFromPrivateDerived from privateDerived, variables x and y are not inherited because they are private variables of privateDerived.

------------------------------------------------------------------------------------------------
7. Explain the difference between:
The private and protected members of a class.
The protected and public members of a class.
ANSWER :
Private : It is an access specifier. By default the instance (member) variables or the methods of a class in c++/java are private. During inheritance , the code and the data is always inherited but is not accessible outside the class. We can declare our data members as private so that no one can make direct changes to our member variables and we can provide public getters and setters in order to change our private members. And this concept is always applied in business rule.
Protected : It is also an access specifier. In c++ , the protected members are accessible within the class and to the inherited class but not outside the class. In java , the protected members are accessible within the class , to the inherited class as well as to all the classes within the same package.
Public members of a class A are accessible for all and everyone.

class Base {
private: int MyPrivateInt;
protected:   int MyProtectedInt;
public: int MyPublicInt;
}
class Derived : Base
{
public:
int foo1() { return MyPrivateInt;} // Won't compile!
int foo2() { return MyProtectedInt;} // OK
int foo3() { return MyPublicInt;} // OK
};
class Unrelated
{
private:   Base B;
public:
int foo1() { return B.MyPrivateInt;} // Won't compile!
int foo2() { return B.MyProtectedInt;} // Won't compile
int foo3() { return B.MyPublicInt;} // OK
};
From above Protected members can be accessed from derived classes. Private ones can't.
------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote