Question

C++: Write a C++ program with user input, output and using inheritance. Please include comments in...

C++:

Write a C++ program with user input, output and using inheritance. Please include comments in your code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Firstly, Inheritance is the technique of deriving a new class from an old one is called inheritance. The old class is referred to as base class and the new class is referred to as derived class or subclass. Inheritance concept allows programmers to define a class in terms of another class, which makes creating and maintaining application easier. When writing a new class, instead of writing new data member and member functions all over again, programmers can make a bonding of the new class with the old one that the new class should inherit the members of the existing class. A class can get derived from one or more classes, which means it can inherit data and functions from multiple base classes.

I am giving you a program which is in C++ and using simple inheritance i.e., reading or taking input as you asked from user and gets it output by calling other function which is derived from the function in which our input values are stored.

I almost included comments and if you require anything more just drop a comment, I will be glad to help you.

Code is as follows:

#include <iostream>
using namespace std;

//Base class
class std_basic_info
{
private:
char name[30];
int age;
char gender;
public:
void getBasicInfo(void);
void putBasicInfo(void);
};

//function definitions
void std_basic_info::getBasicInfo(void)
{
cout << "Enter student's basic information:" << endl;
cout << "Name?: "; cin >> name;
cout << "Age?: "; cin >> age;
cout << "Gender?: ";cin >> gender;
}

void std_basic_info::putBasicInfo(void)
{
cout << "Name: " << name << ",Age: " << age << ",Gender: " << gender << endl;
}

//Derived class
class std_result_info:public std_basic_info
{
private:
int totalM;
float perc;
char grade;
public:
void getResultInfo(void);
void putResultInfo(void);
};

//function definitions
void std_result_info::getResultInfo(void)
{
cout << "Enter student's result information:" << endl;
cout << "Total Marks?: "; cin >> totalM;
perc= (float)((totalM*100)/500);
cout << "Grade?: ";cin >> grade;
}

void std_result_info::putResultInfo(void)
{
cout << "Total Marks: " << totalM << ",Percentage: " << perc << ",Grade: " << grade << endl;
}

int main()
{
//create object of derived class
std_result_info std;

//read student basic and result information
std.getBasicInfo();
std.getResultInfo();

//print student basic and result information
std.putBasicInfo();
std.putResultInfo();

return 0;
}

//I almost included comments and if you require anything more just drop a comment, I will be glad to help you.

Add a comment
Know the answer?
Add Answer to:
C++: Write a C++ program with user input, output and using inheritance. Please include comments in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ Program Write a program that prompts the user to input a number. The...

    Write a C++ Program Write a program that prompts the user to input a number. The program should then output the number and a message saying whether the number is positive, negative, or zero. You must insert the following comments at the beginning of your program and write our commands in the middle: Write a C++ Program: 1 Name: Your Name ID: Your ID #include <iostream> using namespace std; din main YOUR CODE HERE

  • Write a C program as follows: Single source code file Requests the user to input two...

    Write a C program as follows: Single source code file Requests the user to input two integer numbers Requests the user to make a choice between 0 (add), 1 (subtract), or 2 (multiply) Declares three separate functions Uses a pointer to these three functions to perform the requested action Outputs the result to the screen Submit your program source code file to this assignment. Sample Output Enter first integer number: 15 Enter second integer number: 10 Enter Choice: 0 for...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...

    PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...

  • WRITE A PROGRAM IN C/C++ FOR THE QUESTION, INCLUDE COMMENTS, CHECK IF SAMPLE INPUT AND OUTPUT...

    WRITE A PROGRAM IN C/C++ FOR THE QUESTION, INCLUDE COMMENTS, CHECK IF SAMPLE INPUT AND OUTPUT MATCH WITH SAME FORMAT :) Kruskal minimum spanning tree algorithm Description 1013 Investigation province town traffic , urban roads to give existing tables , the table lists the price it takes to connect the two towns . Government "smooth project ," the goal is to make the province between any two towns can implement traffic ( but not necessarily directly connected to the road...

  • Having trouble coding this for C++. Please add comments for better understanding! Write a program that...

    Having trouble coding this for C++. Please add comments for better understanding! Write a program that draws two triangles by using a character 'X' as examples below. The program must prompts a user for an integer larger than 2. The user input will then be used to set the size of the output. If a user input an even value, the program will increase it by one before drawing the box. Example 1: Input: 3 Output: XXX XXX XX XX...

  • Write a C program to “de-vowel” an input string. Assume that the user provides input containing...

    Write a C program to “de-vowel” an input string. Assume that the user provides input containing only the characters a through z (i.e., all lowercase letters). Your program should create an output string that deletes all vowels from the input string, pushing the letters together to fill any gaps. For example, given the input “theturtleandthehare” your code should print out “thtrtlndthhr”. Your program should create an output string from the input string, before printing its output. Sample Input: Enter a...

  • Write a Java program (code + screenshot of output) that takes a user input temperature and...

    Write a Java program (code + screenshot of output) that takes a user input temperature and displays an output as follows: Temperature Output temperature > 40 Hot 40 ≥ temperature > 25 Warm 25 ≥ temperature > 15 Nice 15 ≥ temperature Cold Sample run1: Please input the temperature: 45 Hot Sample run2: Please input the temperature: 40 Warm Sample run3: Please input the temperature: 20 Nice Sample run4: Please input the temperature: 5 Cold Sample run5: Please input the...

  • C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std;...

    C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std; // Write your function here //////////////// STUDENT TESTING //////////////////// int run() { cout << "Student testing" << endl; strip(); return 0; } Here is the instruction: Write a filter function named strip that removes C++ comments from input, sending the uncommented portion of the program to output. Your program should work with both single line (//) comments, and multi-line (/* */) comments. + A...

  • Please write comments so the user will understand the program. The output for the calculation is provided below thw assignment. For this assignment, you will take on the role of a member of an...

    Please write comments so the user will understand the program. The output for the calculation is provided below thw assignment. For this assignment, you will take on the role of a member of an IT department. Your chief technology officer (CTO) has tasked your department with the replacement of IT equipment. Your manager does not want to buy a piece of equipment based on the brand name alone. Rather, your manager wants to know the return on investment for each...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT