1. Mark the following statements as true or false. Consider the following class
ID: 3553049 • Letter: 1
Question
1.
Mark the following statements as true or false.
Consider the following class definition:
class aClass
{
public:
void print() const;
void set(int, int);
aClass();
aClass(int, int);
private:
int u;
int v;
};
What is wrong with the following class definitions?
8.
Consider the following statements:
class yClass
{
public:
void one();
void two(int, int);
yClass();
private:
int a;
int b;
};
class xClass: public yClass
{
public:
void one();
xClass();
private:
int z;
};
Suppose the following statements are in a user program (client code):
yClass y;
xClass x;
Explain the difference between the private and protected members of a class.
16.
What is wrong with the following code?
class classA
{
protected:
void setX(int a); //Line 1
//Postcondition: x = a; //Line 2
private: //Line 3
int x; //Line 4
};
.
.
.
int main()
{
classA aObject; //Line 5
aObject.setX(4); //Line 6
return 0; //Line 7
}
Consider the following code:
class one
{
public:
void print() const;
//Output the values of x and y
protected:
void setData(int u, int v);
//Postcondition: x = u; y = v;
private:
int x;
int y;
};
class two: public one
{
public:
void setData(int a, int b, int c);
//Postcondition: x = a; y = b; z = c;
void print() const;
//Output the values of x, y, and z
private:
int z;
};
784785
What is the output of the following C++ program?
#include <iostream>
#include <string>
using namespace std;
class baseClass
{
public:
void print() const;
baseClass(string s = " ", int a = 0);
//Postcondition: str = s; x = a;
protected:
int x;
private:
string str;
};
class derivedClass: public baseClass
{
public:
void print() const;
derivedClass(string s = "", int a = 0, int b = 0);
//Postcondition: str = s; x = a; y = b;
private:
int y;
};
int main()
{
baseClass baseObject("This is the base class", 2);
derivedClass derivedObject("DDDDDD", 3, 7);
baseObject.print();
derivedObject.print();
return 0;
}
void baseClass::print() const
{
cout << x << " " << str << endl;
}
baseClass::baseClass(string s, int a)
{
str = s;
x = a;
}
void derivedClass::print() const
{
cout << "Derived class: " << y << endl;
baseClass::print();
}
derivedClass::derivedClass(string s, int a, int b)
:baseClass("Hello Base", a + b)
{
y = b;
}
Explanation / Answer
Dear,
1. a. True
b. True
c. True
7.
a.There is a colon missing when deriving bClass from aClass.
Also the class definition is not ended by a semicolon.
b.The parameterized constructer in cClass is not ended with a semicolon.
Also the class definition is not ended by a semicolon.
8.
a. a. a. False.
b.
i. Valid.
ii. Invalid. a and b are private members, so they can be accessed only through member functions but not with instance objects(variables).
iii. Invalid. a and b are private members of yclass which cannot be directly accessed by xclass member function.
iv. Invalid. a, b are private data members of yclass (base-class) which cannot be accessed in derived classes.
16. classA method implementation not given.
17.
a.
void one::setData(int u, int v)
{
x=u;
y=v;
}
void two::setData(int a, int b, int c)
{
z=c;
one ::setData(a,b);
}
b.
void one::print() const
{
cout << x << " " << y << endl;
}
void two::print() const
{
cout << "Derived class: " << z << endl;
one::print();
}
18.
2 This is the base class
Derived class: 7
10 Hello Base
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.