Deck 9: Object Oriented C++
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/8
Play
Full screen (f)
Deck 9: Object Oriented C++
1
Using Inheritance to Create a Derived Class in C++ In this exercise, you use what you have learned about using inheritance to create a derived class to answer Questions.
An advantage of using inheritance is:
A) It maximizes the number of functions.
B) It allows reuse of code.
C) It requires no coding.
An advantage of using inheritance is:
A) It maximizes the number of functions.
B) It allows reuse of code.
C) It requires no coding.
Inheritance:
Inheritance is the process of creating a new class (derived class) from an existing class (base class).
• It allows inheriting the members and properties of a base class from the derived class.
Advantages:
The main advantages of inheritance are,
• Code reusability and fast implementation.
• Reduces the size of program code and saves memory space.
Therefore, an advantage of using inheritance is "it allows reuse of code". Hence, the correct answer is option
.
Inheritance is the process of creating a new class (derived class) from an existing class (base class).
• It allows inheriting the members and properties of a base class from the derived class.
Advantages:
The main advantages of inheritance are,
• Code reusability and fast implementation.
• Reduces the size of program code and saves memory space.
Therefore, an advantage of using inheritance is "it allows reuse of code". Hence, the correct answer is option

.
2
Using Inheritance to Create a Derived Class in C++
In this exercise, you use what you have learned about using inheritance to create a derived class to answer Questions.
True or False: The methods in a derived class have direct access to the base class private data members.
In this exercise, you use what you have learned about using inheritance to create a derived class to answer Questions.
True or False: The methods in a derived class have direct access to the base class private data members.
Derived class creation:
A derived class is defined as a class which is derived from a base class; the derived class can access the member functions and data of the base class.
The syntax to create a derived class from a base class is as follows:
class derivedclass : public baseclass
• Here, the "class" is a keyword to refer the creation of a class.
• The "derivedclass" refers the name of the derived class.
• The 'public" is a keyword which refers the access specifier of the base class.
• The "baseclass" refers the name of the base class.
Accessibility of based class:
The "public" members of a base class can be accessible from anywhere in the class and also from the derived class of the class.
• The methods in a derived class could directly access the "public" data members of the base class.
The "private" members of a base class are "hidden"; it could be accessible only within the base class or "friends" of the base class.
• The derived classes are not able to access the "private" members of the base class.
Consider the following example:
Example program:
// Include required header file
#include
using namespace std;
Define a base class named "base" and define its member functions as "private".
// Base class
class base
{
// private members
private:
// Member function definition
void base_disp()
{
cout "\nBase class function";
}
};
Define a derived class named "derived" and defines its member functions as "public".
// Derived class
class derived : public base
{
// public members
public:
// Member function definition
void derv_disp()
{
Note: The following statement is trying to call a function of base class; but here it throws error because this member function is declared as "private" in the base class.
// Calling a base class's function
base_disp();
cout "\nDerived class function";
}
};
Define a main function to create objects to access functions of the class.
// Function main
int main()
{
// Create a derived class object
derived obj2;
// class a derived class function
obj2.derv_disp();
return 0;
}
Output (Error code):
1 ex3.cpp
1 ex3.cpp(30): error C2248: 'base::base_disp' : cannot access private member declared in class 'base'
1 ex3.cpp(15) : see declaration of 'base::base_disp'
1 ex3.cpp(10) : see declaration of 'base'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hence, the given statement is False.
A derived class is defined as a class which is derived from a base class; the derived class can access the member functions and data of the base class.
The syntax to create a derived class from a base class is as follows:
class derivedclass : public baseclass
• Here, the "class" is a keyword to refer the creation of a class.
• The "derivedclass" refers the name of the derived class.
• The 'public" is a keyword which refers the access specifier of the base class.
• The "baseclass" refers the name of the base class.
Accessibility of based class:
The "public" members of a base class can be accessible from anywhere in the class and also from the derived class of the class.
• The methods in a derived class could directly access the "public" data members of the base class.
The "private" members of a base class are "hidden"; it could be accessible only within the base class or "friends" of the base class.
• The derived classes are not able to access the "private" members of the base class.
Consider the following example:
Example program:
// Include required header file
#include
using namespace std;
Define a base class named "base" and define its member functions as "private".
// Base class
class base
{
// private members
private:
// Member function definition
void base_disp()
{
cout "\nBase class function";
}
};
Define a derived class named "derived" and defines its member functions as "public".
// Derived class
class derived : public base
{
// public members
public:
// Member function definition
void derv_disp()
{
Note: The following statement is trying to call a function of base class; but here it throws error because this member function is declared as "private" in the base class.
// Calling a base class's function
base_disp();
cout "\nDerived class function";
}
};
Define a main function to create objects to access functions of the class.
// Function main
int main()
{
// Create a derived class object
derived obj2;
// class a derived class function
obj2.derv_disp();
return 0;
}
Output (Error code):
1 ex3.cpp
1 ex3.cpp(30): error C2248: 'base::base_disp' : cannot access private member declared in class 'base'
1 ex3.cpp(15) : see declaration of 'base::base_disp'
1 ex3.cpp(10) : see declaration of 'base'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hence, the given statement is False.
3
Using Inheritance to Create a Derived Class in C++
In this exercise, you use what you have learned about using inheritance to create a derived class to answer Questions.
True or False: A derived class may add new methods or override existing methods when inheriting from a base class.
In this exercise, you use what you have learned about using inheritance to create a derived class to answer Questions.
True or False: A derived class may add new methods or override existing methods when inheriting from a base class.
Derived class creation:
A derived class is defined as a class which is derived from a base class; the derived class can access the member functions and data of base class.
The syntax to generate a "derived class" from a "base class" is as follows:
class derivedclass : public baseclass
• Here, the "class" is a keyword to refer the creation of a class.
• The "derivedclass" refers the name of derived class.
• The 'public" is a keyword which refers the access specifier of base class.
• The "baseclass" refers the name of base class.
Properties of derived class:
The "public" members of a base class can be accessible from anywhere in the class and also from the derived class of the class.
• The methods in a derived class could directly access the "public" data members of base class.
The "private" members of a base class are "hidden"; it could be accessible only within the base class or "friends" of base class.
• The derived classes are not able to access the "private" members of base class.
While creating a derived class from a base class, the derived class could add own new methods or it could override the existing methods in the base class.
• After creating the overriding methods, the base class methods can be called by using scope resolution operator "::".
Consider the following example:
Example program:
// Function header
#include
using namespace std;
Define a base class named "base" with one method named "disp()".
// Base class
class base
{
// Access specifier
public:
// Overriding function
void disp()
{
cout "Base class overriding function" endl;
}
};
Define a class named "derived" and inherit base class "base".
// Derived class inherits the base class
class derived: public base
{
// Access specifier
public:
// Overriding function
void disp()
{
cout "Derived class overriding function" endl;
// Accessing the overriding function using
// scope resolution operator
base::disp();
}
};
Define a main function to create class objects and call the overriding functions.
// Function main
int main(void)
{
// Create object for the derived class
derived obj;
Here, the function call statement invokes only the derived class overriding function.
// Make a class to the function disp()
obj.disp();
// Pause the output screen
system("pause");
}
Output:
Derived class overriding function
Base class overriding function
Press any key to continue...
Therefore, while inheriting a "base class", a "derived class" could add new methods or override existing methods in base class.
Hence, the given statement is True.
A derived class is defined as a class which is derived from a base class; the derived class can access the member functions and data of base class.
The syntax to generate a "derived class" from a "base class" is as follows:
class derivedclass : public baseclass
• Here, the "class" is a keyword to refer the creation of a class.
• The "derivedclass" refers the name of derived class.
• The 'public" is a keyword which refers the access specifier of base class.
• The "baseclass" refers the name of base class.
Properties of derived class:
The "public" members of a base class can be accessible from anywhere in the class and also from the derived class of the class.
• The methods in a derived class could directly access the "public" data members of base class.
The "private" members of a base class are "hidden"; it could be accessible only within the base class or "friends" of base class.
• The derived classes are not able to access the "private" members of base class.
While creating a derived class from a base class, the derived class could add own new methods or it could override the existing methods in the base class.
• After creating the overriding methods, the base class methods can be called by using scope resolution operator "::".
Consider the following example:
Example program:
// Function header
#include
using namespace std;
Define a base class named "base" with one method named "disp()".
// Base class
class base
{
// Access specifier
public:
// Overriding function
void disp()
{
cout "Base class overriding function" endl;
}
};
Define a class named "derived" and inherit base class "base".
// Derived class inherits the base class
class derived: public base
{
// Access specifier
public:
// Overriding function
void disp()
{
cout "Derived class overriding function" endl;
// Accessing the overriding function using
// scope resolution operator
base::disp();
}
};
Define a main function to create class objects and call the overriding functions.
// Function main
int main(void)
{
// Create object for the derived class
derived obj;
Here, the function call statement invokes only the derived class overriding function.
// Make a class to the function disp()
obj.disp();
// Pause the output screen
system("pause");
}
Output:
Derived class overriding function
Base class overriding function
Press any key to continue...
Therefore, while inheriting a "base class", a "derived class" could add new methods or override existing methods in base class.
Hence, the given statement is True.
4
Creating a Class in C++
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
What is the output when the following line of C++ code executes?
cout ? "The circumference is : " ? oneCircle.calculateCircumference();
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
What is the output when the following line of C++ code executes?
cout ? "The circumference is : " ? oneCircle.calculateCircumference();
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
5
Creating a Class in C++
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
Is the following a legal C++ statement? Why or why not?
cout ? "The area is : " ? calculateArea();
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
Is the following a legal C++ statement? Why or why not?
cout ? "The area is : " ? calculateArea();
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
6
Creating a Class in C++
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
Consider the following C++ code. What is the value stored in the oneCircle object's
attribute named radius?
oneCircle.setRadius(10.0);
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
Consider the following C++ code. What is the value stored in the oneCircle object's
attribute named radius?
oneCircle.setRadius(10.0);
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
7
Creating a Class in C++
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
Write the C++ code that will assign the circumference of oneCircle to a double variable named circumference1.
In this exercise, you use what you have learned about creating and using a programmer-defined class. Study the following code, and then answer Questions.
class Circle{public:void setRadius(double);double getRadius();double calculateCircumference();double calculateArea();private:double radius; // Radius of this circleconst double PI = 3.14159;};void Circle::setRadius(double rad){radius = rad;}double Circle::getRadius(){return radius;}double Circle::calculateCircumference(){return (2 * PI * radius)}double Circle::calculateArea(){return(PI * radius * radius)}
In the following exercise, assume that a Circle object named oneCircle has been created in a program that uses the Circle class, and radius is given a value as shown in the following code:
Circle oneCircle;oneCircle.setRadius(4.5);
Write the C++ code that will assign the circumference of oneCircle to a double variable named circumference1.
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
8
Using Inheritance to Create a Derived Class in C++ In this exercise, you use what you have learned about using inheritance to create a derived class to answer Questions.
Which line of code is used to create a derived class named SubWidget from a base class named Widget?
A) class Widget : public SubWidgetb. class SubWidget : base public Widgetc. class Widget : derived public SubWidgetd. class SubWidget : public Widget
Which line of code is used to create a derived class named SubWidget from a base class named Widget?
A) class Widget : public SubWidgetb. class SubWidget : base public Widgetc. class Widget : derived public SubWidgetd. class SubWidget : public Widget
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck