Each of the following programs illustrates one or more concepts or issues in C++
ID: 3533288 • Letter: E
Question
Each of the following programs illustrates one or more concepts or issues in C++. What
is/are those issues? What is the result of compiling and running each program? (Some
programs may not compile.) Explain your answer. Assume iostream and namespace
std are included in each program. Any minor syntax errors are typos.
6. a)
void you( long a, long b) {cout << "long";}
void you( double a, double b) {cout << "double";}
int main(){
int a, b;
you( a, b );
}
b)
int main(){
const char* what = "Is This";
what = "Interesting";
cout << *what;
what[3] = 'a';
cout << *what;
}
7. a)
int& Now() {
int Where = 1;
return Where ;
}
int main() {
int Where;
Where= Now();
cout << Where;
}
b)
class Where {
public:
int here;
int foo() const;
int bar() const;
};
int Where ::foo() const {
here = here + 5;
return here;
}
int Where ::bar() const {
return here + 5;
}
int OhNo(const Where* Now) {
return Now->bar();
}
int main(){
Where* IsIt = new Where;
cout << OhNo(IsIt);
};
Page 2 of 12
8. a)
class Top {
public:
int a;
Top(int x) { a = x;}
};
class Bottom : public Top {
public:
int b;
Bottom() : Top(5) { b = 0; a = 0;}
};
int main() {
Bottom barrel;
cout << barrel.a <<" " << barrel.b;
}
b)
class Top
{
public:
void Hat(char *string) { cout << "Top";}
void Hat(float a) { cout << "Top Too";}
};
class Bottom : public Top
{
public:
void Hat(const int a) { cout << "Bottom";}
void Hat(float a) { cout << "Bottom Too";}
};
int main(){
Bottom* Rung;
Rung->Hat(5.5);
Rung->Hat("cat");}
9.
class Top
{ public:
Top() { cout << "Start Top ";}
~Top() { cout << "End Top ";}
};
class Bottom : public Top
{ public:
Bottom() { cout << "Start Bottom ";}
~Bottom() { cout << "End Bottom ";}
};
class Test
{ public:
Bottom deal;
Test() { cout << "Start Test ";}
};
int main(){
Test me;
}
10.Give the output of the following program.
#include <iostream>
using namespace std;
class Top {
public:
virtual void MyMemory() { cout << "I forget" << endl;};
void Disk() { cout << "Space" << endl;};
void Erased() { cout << "For good" << endl;};
void ThisExam() { Erased(); MyMemory(); };
virtual ~Top() {}
};
class Bottom : public Top {
public:
void MyMemory() { cout << "Gone" << endl;};
void Disk() { cout << "Slipped" << endl;};
void virtual Erased() { cout << "Rubbed out" << endl;};
};
int main()
{
Top* Hat = new Bottom;
Hat->MyMemory();
Hat->Disk();
Hat->ThisExam();
Top Dog = *(new Bottom);
Dog.MyMemory();
Dog.Disk();
Dog.ThisExam();
}
Explanation / Answer
issues 6a)a,b are not assigned.also type conversion could be a problem. and could casue ambiguity
6b)what[3]='a' is wrong as it involves assignment from read only location
7a) it is compiling
b) ; after int main() and scope issues with here.also i think const should be used before int
8a) it is compiling
b) call of hat is ambigious and invalid conversion of const char to int
9) it is compliling
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.