Concept explainers
What will the following
#include <iostream>
#include <memory>
using namespace std;
class First
{
protected:
int a;
public:
First(int x = 1) { a = x; }
int getVal() const { return a; }
};
class Second : public First
{
private:
int b;
public:
Second(int y = 5) { b = y; }
int getVal() const { return b; }
};
int main()
{
Shared_ptr<First> object1 = make_shared<First>();
shared_ptr<Second> object2 = make_shared<Second>():
cout << object1->getVal() << endl;
cout << object2−>getVal() << endl:
return 0;
}
Want to see the full answer?
Check out a sample textbook solutionChapter 15 Solutions
STARTING OUT WITH C++ MPL
Additional Engineering Textbook Solutions
Electric Circuits. (11th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Web Development and Design Foundations with HTML5 (8th Edition)
Database Concepts (8th Edition)
- C++arrow_forwardExplain below code #include <iostream>#include <cmath>using namespace std; const int DIAMETER = 20; // diameter of each ringconst int SPACE = 5; // space between rings class Circle{ public: void drawOlympicRings(int RINGS) { for (int y = 0; y < DIAMETER; y++) { for (int x = 0; x < RINGS * (DIAMETER + SPACE) - SPACE; x++) { bool draw = false; // calculate the distance from the center of each ring for (int i = 0; i < RINGS; i++) { int cx = (DIAMETER / 2) + i * (DIAMETER + SPACE); int cy = DIAMETER / 2; int dx = x - cx; int dy = y - cy; float d = sqrt(dx * dx + dy * dy); if (d <= DIAMETER / 2) { draw = true;…arrow_forwardQ No 3. #include <string> using namespace std; class Account { public: Account(string accountName, int initialBalance) { name=accountName; if (initialBalance > 0) { balance = initialBalance; } } void deposit(int depositAmount) { if (depositAmount > 0) { balance = balance + depositAmount; } } int getBalance() const { return balance; } void setName(string accountName) { name = accountName; } string getName() const { return name; } private: string name; int balance; }; Rewrite the following code in correct form. a) Assume the following prototype of destructor is declared in class Time: void ~Time(int); b) Assume the following prototype of constructor is declared in class Employee: int Employee(string, string);…arrow_forward
- Class Implementation Exercises EX1: Write a complete C++ program to implement a car class. The class has three attributes: Id, speed and color. Also, the class include the following methods: (setter) function, (getter) function, (default and parameterized constructor), and( print) to print the attributes values ➜ Create three objects in the main function then call the method. EX2: Write a complete C++ program to implement a student class. ➜ The class has three attributes:std-Id, Name and marks [3]. Also, the class include the following methods: (setter) function,(getter) functions (default and parameterized constructor), (average) and(print) to print the attributes values ➜ Create array of objects in the main function then call the method.arrow_forwardTrue or false ? Assuming class Quiz { public: void TakeIt ( ); }; Quiz cs2b_quiz; Quiz * p_quiz = & cs2b_quiz; The syntax to invoke TakeIt ( ) member function using the pointer is: p_quiz->TakeIt ( );arrow_forwardclass Vehicle { private: int wheels; protected : int passenger: public : void inputdata(int, int); void outputdata(); }; class Heavyvehicle : protected Vehicle { int diesel_petrol; protected : int load; public: void readdata(int, int); void writedata(); }; class Bus : private Heavyvehicle char make[20]; public : void fetchdata(char); void displaydata(); (1) Name the base class and derived class of the class Heavyvehicle. (ii) Name the data member(s) that can be accessed from function displaydata(). (iii) Name the data member's that can be accessed by an object of Bus class. (iv) Is the member function outputdata() accessible to the objects of Heavyvehicle class.arrow_forward
- #include #include #include #include using namespace std; class AttackInfo{ public: }; int dmg; string description; AttackInfo() AttackInfo(int a, string b){ dmg = a; description = b; class BaseCharacter{ private: } string name; int maxHP,presentHP,dodgeChance; public: int strength, agility,intelligence, baseDamage; public: BaseCharacter() { BaseCharacter(string a, int b, int c, int d, int e, int f, int g){ name = a; strength = b; agility = c; intelligence = d; maxHP = e; presentHP = maxHP; dodgeChance = f; baseDamage=g; } AttackInfo primaryAttack(){ return AttackInfo(baseDamage,"strikes at"); } at"); AttackInfo secondaryAttack(){ return AttackInfo(baseDamage,""); } } void takeDamage(int a){} string toString(){ return "Name: "+getName()+"Max HP: "+to_string(getMaxHp())+" Present HP: "+to_string(getPresentHP())+"Dodge Chance: "+to_string(get DodgeChance()); } }; AttackInfo getAttack(){ return primaryAttack(); }; class Warrior: public BaseCharacter{ private: string getName(){return name;}…arrow_forwardC++ #include <string>#include <cmath> class DollarAmount {public:// initialize amount from an int64_t valueexplicit DollarAmount(int64_t value) : amount{value} { } // add right's amount to this object's amountvoid add(DollarAmount right) {// can access private data of other objects of the same classamount += right.amount; } // subtract right's amount from this object's amountvoid subtract(DollarAmount right) {// can access private data of other objects of the same classamount -= right.amount;} // uses integer arithmetic to calculate interest amount, // then calls add with the interest amountvoid addInterest(int rate, int divisor) {// create DollarAmount representing the interestDollarAmount interest{(amount * rate + divisor / 2) / divisor}; add(interest); // add interest to this object's amount} // return a string representation of a DollarAmount objectstd::string toString() const {std::string dollars{std::to_string(amount / 100)};std::string…arrow_forwardModify the Student class presented above as follows. Each student object should contain the scores for three (3) tests. (1) Provide a constructor that sets all instance values based on parameter values. (2) ✓ Overload the constructor such that each test score is assumed to be initially zero. (5) Provide a method called setTestScore that accepts two (2) parameters: the test number (1 through 3) and the score. Also provide a method called getTestScore that accepts the test number and returns the appropriate score. (6) Provide a method called average that computes and returns the average test score for the student. (3) Modify the display method such that the test scores and average are included in the description of the students. (5) Implement the class to exercise the methods of Student. (8) NOTE WELL ✓ Ensure that your programs are documented (comments) as needed ✓ Make certain that your programs are debugged and working before submittingarrow_forward
- #include <iostream> using namespace std; class Product { private: double price; int weight; public: void info() { cout <<price<<", "<<weight; } }; class Fruit { public: string type; void setInfo(double p, int w) { price = p; weight = w; } }; int main() { Fruit obj; obj.type = "Apple"; obj.setInfo(4.99, 10); obj.info(); }arrow_forwardY3arrow_forwardJava:arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education