Concept explainers
Explanation of Solution
Creating pointers to base class:
The pointer object can be created to a base class and it uses “*” before the object. This object can be assigned to the address of the derived class.
Program:
The below program explains the creation of pointer object to the base class.
/*Include required files*/
#include<iostream>
using namespace std;
/*Base Class*/
class One
{
/*Access specifier*/
protected:
/*Declare the variable "base_var" as "int"*/
int base_var;
/*Access specifier*/
public:
/*Constructor*/
One (int num = 10)
{
/*Assign val to base_var*/
base_var = num;
}
/*Function getValue()*/
int getValue()
{
/*Return the value of base_var*/
return base_var;
}
};
/*Derived class*/
class Two: public One
{
/*Access specifier*/
private:
/*Declare the variable "der_var" as "int"*/
int der_var;
/*Access specifier*/
public:
/*Constructor*/
Two(int num = 30 )
{
/*Assign “val” to “der_var” */
der_var = num;
}
/*Function getValue()�...
Want to see the full answer?
Check out a sample textbook solutionChapter 15 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
- To overload a function in a child class, inherited from its base class, the function must be a which one: virtual function overridden function binary function inherited functionarrow_forwardA nonmember function must be declared as a(n)_________ of a class to have access tothat class’s private data members.arrow_forwardExplain why memberwise assignment in a class with a pointer member may create issues.arrow_forward
- Using Base class pointer create a derived class dynamic object. Set values of dynamically created object using base class pointer (use arrow operator with pointer of base class). Call Area function of derived class using base class pointer. Delete pointer of base class as it has fulfilled its duty. CODE GIVEN BELOW #include <iostream> using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area () { return (0); } }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon poly; CPolygon * ppoly1 = ▭ CPolygon * ppoly2 = &trgl; CPolygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << endl; cout <<…arrow_forwardDesign a class called Bird that includes three pieces of information as data members-name (type string), species (type string) and a sizeInInches (type int).Define virtual function getCageSize ().Your class should have a constructor that initializes the three data members. Provide setter and getter function for each data member.Overload the virtual function of inherited class. Write a main part which creates the Bird class and a derived class Vulture.arrow_forwardExplain why memberwise assignment can cause problems with a class that contains a pointer member.arrow_forward
- Create a class: Doctor, with private data members: id, name, specialization and salary with public member function: get_data () to take input for all data members, show_data () to display the values of all data members and get specialization () to return specialization. Write a program to write for detail of n doctors by allocating the memory at run time only. Write the program in c++ language. ( Hint: Use the concept of dynamic memory allocation )arrow_forwardClass members specified as________ are accessible only to member functions of the class and friend s of the class.arrow_forwardCreate a class called ADDITION has one integer and one float data member. Use pointer to data member to find their sum and print it from main method and also use the concept of dynamic write the program in C++arrow_forward
- When using pointers or references to a base class, how should software invoke the same virtual function in different forms and structures?arrow_forwardplz write in java language i urgently need it Write a base class Computer that contains data members of wordsize(in bits), memorysize (in megabytes), storagesize (in megabytes) and speed (in megahertz). Derive a Laptop class that is a kind of computer but also specifies the object’s length, width, height, and weight. Member functions for both classes should include a default constructor, a constructor to initialize all components and a function to display data membersarrow_forward1. A___________is a special member function used to initialize the data members of a class.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,