Self-test 1:
C++ Class Basics
-
If a member of a class is ____, you cannot access it outside the class.
- static
- public
- private
- constant
Solutionprivate
-
In C++, class is a reserved word, and it defines only a(n) ____; no memory is allocated.
Solutiondata type
-
If a class object is passed by _____, the contents of the member variables of the actual parameter are copied into the corresponding member variables of the formal parameter.
- value
- reference
- either value or reference
- None of the above
Solutionvalue
-
The word ____ at the end of a member function heading specifies that the function cannot modify the member variables of that object.
- protected
- private
- public
- const
Solutionconst
-
If a member function is used only to implement other member functions of a class and the user does not need to access the function, you should declare it ____
Solutionprivate
-
Which of the following is true regarding parameter passing with class objects?
- A class object may be passed by reference only.
- A class object may be passed by value only.
- A class object may be passed by reference or by value.
- A class object may not be passed as a parameter.
SolutionA class object may be passed by reference or by value.
-
A constructor:
- Obtains memory for a new variable
- Enables the initialization of an object as it is created
- Constructs a new data type
- None of the above
SolutionEnables the initialization of an object as it is created
-
The members of a class, by default, are:
- public
- protected
- private
- mandatory to specify
Solutionprivate
-
What is the output of the following code
#include <iostream> using namespace std; inline int cubic(int s) { return s*s*s; } int main() { cout << "The cubic of 3 is: " << cubic(3) << "\n"; return 0; }
SolutionThe cubic of 3 is: 27
-
What is the output of the following program when 45 and 15 are entered (in the order)?
#include <iostream> using namespace std; class Operation { int a, b, aa, ss, mm, vv; public: void get(); void su(); void di(); void pr(); void dv(); }; inline void Operation::get() { cout << "Enter first value:"; cin >> a; cout << "Enter second value:"; cin >> b; } inline void Operation::su() { aa = a+b; cout << ": " << a+b << "\n"; } inline void Operation::di() { ss = a-b; cout << ": " << a-b << "\n"; } inline void Operation::pr() { mm = a*b; cout << ": " << a*b << "\n"; } inline void Operation::dv() { vv = a/b; cout << ": " << a/b << "\n"; } int main() { cout << "Program using inline function\n"; Operation s; s.get(); s.su(); s.di(); s.pr(); s.dv(); return 0; }
Solution: 60 : 30 : 675 : 3
-
What is the output of the following program?
#include <iostream> using namespace std; class Test { private: int x; public: Test(int x = 0) { this->x = x; } void change(const Test *t) { *this = *t; } void print() const { cout << "x = " << x << endl; } }; int main() { Test obj(5); Test* ptr = new Test (10); obj.change(ptr); obj.print(); return 0; }
Solutionx = 10
-
In the question above, the body of the constructor of Test reads: this->x = x; What does the first and second x refer to?
- the first x is the class member variable
- the second x is the class member variable
- the first x is the function input argument
- the second x is the function input argument
SolutionThe first x is the class member variable, and the second is the function input argument
-
In the following program, is the line: obj1.setX(10).setY(20); correct? Why?
#include<iostream> using namespace std; class Test { private: int x; int y; public: Test(int a = 0, int b = 0) { x = a; y = b; } Test& setX(int a) { x = a; return *this; } Test& setY(int b) { y = b; return *this; } void print() const { cout << "x = " << x << " y = " << y << endl; } }; int main() { Test obj1(5, 5); obj1.setX(10).setY(20); obj1.print(); return 0; }
SolutionYes; When a reference to a non-local object is returned, the returned reference can be used to chain function calls on a single object. Here all calls modify the same object as the same object is returned by reference.
-
What is the output of the following C++ program?
#include <iostream> class A { public: A(int n = 0) : m(n) { std::cout << 'd'; } A(const A& a) : m(a.m) { std::cout << 'c'; } private: int m; }; void f(const A& a1, const A& a2 = A()) { } int main() { A a(2), b; const A c(a), &d = c, e = b; b = d; A* p = new A(c), *q = &a; static_cast < void >(q); delete p; f(3); std::cout << std::endl; return 0; }
Solutionddcccdd