Explore BrainMass
Share

C++ Program

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

Looking for the solutions to these C++ sample study problems to assist me in understanding how these problems are solved using C++ for inheritance.

See the attached file for full problem.

© BrainMass Inc. brainmass.com March 21, 2019, 10:36 pm ad1c9bdddf
https://brainmass.com/computer-science/cpp/cplusplus-study-examples-inheritance-423593

Attachments

Solution Preview

Please see attached for the solution to the following problems.

C++ Study Examples - Inheritance

1. What must be true for a class to be considered an abstract class?

Answer: An class will be an abstract class if it has at least one pure virtual function. This can be achieved by using a pure specifier (= 0).

2. Consider a class declaration that begins

class Derived : public Base
{. . .

Determine whether each of the following statements is true or false.

a. The public members of Base become public members of Derived.
b. The public members of Derived become public members of Base.
c. The protected members of Base become protected members of Derived.
d. The private members of Derived become public members of Base.
e. The private members of Base remain private to Base, and are inaccessible to Derived.

Answers:
a. TRUE
b. FALSE
c. TRUE
d. FALSE
e. TRUE

3. Given the following class definition:

class B
{
public:
int e();
void f();
private:
int x;
};
class D : public B
{
public:
void h();
void g();
private:
float y;
}

How many public methods does class D have? List them.

Answer: Class D has 4 public methods, since it has 2 declared public methods and additional 2 methods it inherited from its base class. These methods are e(), f(), g() and h().

4. For each of the following, state whether the terms have an "is-a" or "has-a" relationship, and explain your answer.

a. State University and the School Of Dentistry
b. Superhero and Batman
c. Student and Student Number
d. Student and Undergraduate

Answers:
a. "has-a". It is because school of dentistry is an entity within a state university.
b. "is-a". It is because Batman is a type of superhero.
c. "has-a". It is because a student number is one of the entities associated with a student.
d. "is-a". It is because an undergraduate is a type of student.

5. Modify your previous StudentGrade class so that all methods are marked const that can be. Make sure you have a constructor that accepts all data values. Create a simple main function that declares a const StudentGrade object and demonstrates that the values have been successfully stored inside.

Answer: Below is the modified StudentGrade code that uses a constructor and has const on all applicable methods and the main object class:

#include<iostream>

using namespace std;

class StudentGrade
{
public:
StudentGrade(int s, int id)
{
exam_score = s;
if(exam_score<0 && exam_score>100)
{
exam_score = 0;
cout<<"Entered score is invalid. Score resets to 0.n";
}

id_number = id;
if(id_number<10000 && id_number>50000)
{
id_number = 10000;
cout<<"Entered ID number is invalid. Id number resets to
10000.n";
}
}

int getScore() const
{
return exam_score;
}

int getIDNumber() const
{
return id_number;
}

void displayRecord() const
{
cout << "Student "<<id_number<<" has a score of
"<<exam_score<<".n";
}

private:
int exam_score;
int id_number;
};

int main()
{
int score, student_id;

cout<<"Enter Student ID Number: ";
cin>>student_id;

cout<<"Enter Test Score: ";
cin>>score;
const StudentGrade sg (score, student_id);

sg.displayRecord();

cin.ignore();
cin.get();

return 0;
}

6. Working from the last problem, create a StudentMajor class with "get" and "set" methods (the major itself is a string). Add a StudentMajor data member to your StudentGrade class, and add methods to StudentGrade to get and set this member.

Answer: Below is the modified StudentGrade program with additional StudentMajor
class (non-derived):

#include<iostream>
#include<string>

using namespace std;

class StudentGrade
{
public:
StudentGrade(int s, int id)
{
exam_score = s;
if(exam_score<0 && exam_score>100)
{
exam_score = 0;
cout<<"Entered score is invalid. Score resets to 0.n";
}

id_number = id;
if(id_number<10000 && id_number>50000)
{
id_number = 10000;
...

Solution Summary

This posting contains the solution to the given c++ problems.

$2.19