Concept explainers
Listed below are definitions of two classes that use inheritance, code for their implementation, and a main function. Put the code into appropriate files with the necessary include statements and preprocessor statements so that the program compiles and runs. It should output “Circle has radius 2 and area 12.5664”.
class Shape { public: Shape(); Shape(string name); string getName(); void setName(string newName); virtual double getArea() = 0; private: string name; }; Shape::Shape() { name=""; } Shape::Shape(string name) { this->name = name; } string Shape::getName() { return this->name; } void Shape::setName(string newName) { this->name = newName; } class Circle : public Shape { public: Circle(); Circle(int theRadius); void setRadius(int newRadius); double getRadius(); virtual double getArea(); private: int radius; }; Circle::Circle() : Shape("Circle"), radius(0) { } Circle::Circle(int theRadius) : Shape("Circle"), radius(theRadius) { } void Circle::setRadius(int newRadius) { this->radius = newRadius; } double Circle::getRadius() { return radius; } double Circle::getArea() { return 3.14159 * radius * radius; } int main() { Circle c(2); cout << c.getName() << " has radius " << c.getRadius() << " and area " << c.getArea() << endl; return 0; } |
Add another class, Rectangle, that is also derived from the Shape class. Modify the Rectangle class appropriately so it has private width and height variables, a constructor that allows the user to set the width and height, functions to retrieve the width and height, and an appropriately defined getArea function that calculates the area of the rectangle.
The following code added to main should output “Rectangle has width 3 has height 4 and area 12.0”.
Rectangle r(3,4); cout << r.getName() << " has width " << r.getWidth() << " has height " << r.getHeight() << " and area " << r.getArea() << endl; |
Want to see the full answer?
Check out a sample textbook solutionChapter 15 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Concepts Of Programming Languages
Introduction To Programming Using Visual Basic (11th Edition)
Computer Science: An Overview (12th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Software Engineering (10th Edition)
Digital Fundamentals (11th Edition)
- User-defined Class: You will design and implement your own data class. The class will store data that has been read as user input from the keyboard (see Getting Input below), and provide necessary operations. As the data stored relates to monetary change, the class should be named MoneyChange. The class requires at least 2 instance variables for the name of a person and the coin change amount to be given to that person. You may also wish to use 6 instance variables to represent amounts for each of the 6 coin denominations (see Client Class below). There should be no need for more than these instance variables. However, if you wish to use more instance variables, you must provide a legitimate justification for their usage in the internal and external documentation.Your class will need to have at least a default constructor, and a constructor with two parameters:one parameter being a name and the other a coin amount. Your class should also provide appropriate get and set methods for…arrow_forwardConvert the pseudocode into Python. 1. Pet ClassDesign a class named Pet, which should have the following fields:■ name: The name field holds the name of a pet.■ type: The type field holds the type of animal that a pet is. Example values are "Dog", "Cat", and "Bird".■ age: The age field holds the pet’s age.2. The Pet class should also have the following methods:■ setName: The setName method stores a value in the name field.■ setType: The setType method stores a value in the type field.■ setAge: The setAge method stores a value in the age field.■ getName: The getName method returns the value of the name field.■ getType: The getType method returns the value of the type field.■ getAge: The getAge method returns the value of the age field.3. Once you have designed the class, design a program that creates an object of the class and prompts the user to enter the name, type, and age of his or her pet. This data should be stored in the object. Use the…arrow_forwardThe class definition for an Item is given in the image UML diagram below Note: getInfo() returns a string containing ALL of the state information neatly organized. Part 1: Create a class based on the specification above. Reminder: – means private and + means public. Part 2: a)Write a function/method called “addItem” that takes: An array of items The number of items in the array An integer representing a barcode A string representing the name of an item A string representing the description of an item A double value representing the price The function must create and add the item to the array if there is space. The function must return “true” if the addition was successful and “false” otherwise. b)Write a function/method called “listItems” that takes: An array of items The number of items in the array The function must return a string containing the information on each item in the array. c)Write a function called “pricelookup” that takes : An array of items The number of…arrow_forward
- Please code in python Create a program with two classes – the person class and the student The person class has the following properties: first name (first_name), last name (last_name) street address (address) city (city) zip code (zip) The class has the following methods: get_full_name, which returns the full name of a person get_full_address, which return greeting, returns a greeting message. The class should provide accessor and mutator for each property The class should override the __str__ method to return the state of the object. Create a child class called student which has a property named graduation year (graduation_year) and major. Provide accessor and mutator for each property of its own It inherits all the properties and methods of the person parent class as well. Create a test program that Create an object of the person class and print the full name of a person. Create an object of the person class and print…arrow_forwardUser-defined Class:You will design and implement your own data class. The class will store data that has been read asuser input from the keyboard (see Getting Input below), and provide necessary operations. As thedata stored relates to monetary change, the class should be named Change. The class requires atleast 2 instance variables for the name of a person and the coin change amount to be given to thatperson. You may also wish to use 4 instance variables to represent amounts for each of the 4 coindenominations (see Client Class below). There should be no need for more than these instancevariables. However, if you wish to use more instance variables, you must provide legitimatejustification for their usage in the internal and external documentation.Your class will need to have at least a default constructor, and a constructor with two parameters:one parameter being a name and the other a coin amount. Your class should also provide appropriateget and set methods for client usage. Other…arrow_forwardJava:arrow_forward
- Problem C • -3: method consonants() had more than one loop • -3: method initials () had more than one loop • -3: at least one method used nested loopsarrow_forwardWrite program in C++ Language The other solutions on chegg were incorrect! Please read carefully! Write a program that defines and tests a class called COP3014. The class resembles aclassroom. It has the following grading scheme:3 quizzes 20 points each 20%Mid term 100 points 30%Final 100 points 50%Based on the total grade of any student for the course, the letter grade of each student can becomputed as follows:Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and anygrade below 60 is an F.This class needs to have some member variables. The first name, last name, the Z-number, thegrades, the total grade and final letter grade are all considered to be private member variablesof this class.The class should also have the following public member functions/procedural attributes that canbring the objects (students of the class) to life and give them some…arrow_forwardbuild a student class implement the student class with the following instance variables: * id * firstName *lastName *dateOfBirth * Major create an __init__ function adn initialize all the fields make the "major" an option field and set a default value to "undefined" create a setter and getter function for all these five variables create another function: "print_student_info()" which prints 5 pieces of information: id, first name, last name, date of birth, and major. Make sure to have proper formatting done for printing these 5 things. Use the student class Task 1: create an empty list named "all_students" Task 2: create a variable named "id" and initialize it to 100 (some default value to start with, next id would be 101) Task 3: ask the user for input "How many students:" Task 4: Now run a for loop based on that input number and do the following things for each iteration: - get input of student's first name, last name, date of birth, and major - user should be able to skip the…arrow_forward
- Pythonarrow_forwardexactly as mentioned:arrow_forwardAs a starting point, use the files fraction.h, fraction.cpp, testfraction.cpp below: // simple class for fractions #ifndef FRACTION_H_#define FRACTION_H_ class Fraction { private: // internal implementation is hidden int num; // numerator int den; // denominator int gcd(int a, int b); // calculates the gcd of a and b int lcm(int a, int b); public: Fraction(); // empty constructor Fraction(int, int = 1); // constructor taking values for fractions and // integers. Denominator by default is 1 void print(); // prints it to the screen}; #endif /* FRACTION_H_ */ #include <iostream>#include "fraction.h" Fraction::Fraction(){ num = 1; den = 1;} Fraction::Fraction(int n, int d){ int tmp_gcd = gcd(n, d); num = n / tmp_gcd; den = d / tmp_gcd;} int Fraction::gcd(int a, int b){ int tmp_gcd = 1; // Implement…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning