1(A)- Modify the following cout statments to support the format specified: char
ID: 3818698 • Letter: 1
Question
1(A)- Modify the following cout statments to support the format specified: char name[41] = "Fred"; double rate = 12.34; cout << "*" << name << "*" << endl;; cout << "*" << rate << "*" << endl; * Fred*, right justified, 40 spaces *000012.340*, right justified, 3 digits after decimal, 10 spaces padded with 0
(B)- If Class Base has a virtual function called act(), and the Class Derived which inherits the Base class has a function called act(). 1- Is act() of Derived virtual too? 2- If Derived has another function and calls act() which act will be called, Base’s or Derived’s? How can you call act() in this function to make sure the Base’s act() is called
Explanation / Answer
1A
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char name[41] = "Fred";
double rate = 12.34;
cout << setfill(' ') << setw(41) << "*"<<name<<"*"<<endl;
cout<< "*"<<fixed<<setprecision(3)<<setw(10);
cout << setfill('0') << rate << "*" << endl;
return 0;
}
output:
*Fred*
*000012.340*
B
#include <iostream>
using namespace std;
class Base
{
public:
virtual void act()
{
cout<<" act() in base";
}
};
class Derived : public Base
{
public:
void act()
{
cout<<"act() inside Derived class";
}
void display()
{
act(); // will call act() of Derived class
Base::act(); //Base class act() will be called
}
};
int main()
{
Derived d;
d.act();//derived class act() will be called
d.Base::act(); //Base class act() will be called
d.display();
return 0;
}
Output:
act() inside Derived class
act() in base
act() in base
1. No act() of Derived is not virtual .
2. Derived class act().
3.use Base:: act() class specifier
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.