Describe how to construct the header file of a derived class.

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 12RQ
icon
Related questions
Question

Describe how to construct the header file of a derived class.

Expert Solution
Step 1: Explanation

Let’s assume class “one” is the base class and class “two” is the derived class. To avoid multiple inclusions of these classes, use the following preprocessor commands:

#ifndef one_H

It means that the class “one” is not defined and cannot access the identifiers from the class “one”.

#define one_H

After executing the above statement, the compiler notices that there is no such existing file, hence defines “one_H”

#endif

This command is the end of the “ifndef” command.

Consider an example:

Using preprocessor commands, define “one.h”

//Header files

#ifndef one_H

//define a macro for class “one”

#define one_H

//define a class one

class one {

public:

//declare member functions

int sumDigits();

one():

private:

//declare member variables

int b;

int a:

};

//end the definition of macro of class

#endif

Using preprocessor commands define “two.h” which is derived from class “one”

//Include header files

#ifndef two_H

//define a macro for class two

#define two_H

#include "one.h"

//define a class two from “one” class

class two: public one {

Place the definitions of the class two here

};

//end the definition of macro of class

#endif

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr