This document contains the answers to an in-class exercise on advanced programming. It defines key concepts like class vs object, and public, private, and protected class declarations. It also answers questions about using classes and objects, like class and object names, constructors, destructors, member variables and functions. The questions cover iostream libraries, namespaces, class declarations and definitions, default and parameterized constructors, and calling member functions to get or set values of objects.
This document contains the answers to an in-class exercise on advanced programming. It defines key concepts like class vs object, and public, private, and protected class declarations. It also answers questions about using classes and objects, like class and object names, constructors, destructors, member variables and functions. The questions cover iostream libraries, namespaces, class declarations and definitions, default and parameterized constructors, and calling member functions to get or set values of objects.
This document contains the answers to an in-class exercise on advanced programming. It defines key concepts like class vs object, and public, private, and protected class declarations. It also answers questions about using classes and objects, like class and object names, constructors, destructors, member variables and functions. The questions cover iostream libraries, namespaces, class declarations and definitions, default and parameterized constructors, and calling member functions to get or set values of objects.
1. a) What is the difference between class and object?
A: Class is the type of the object. Object is the actual realization of the class (variable_type variable_name). b) When and for what is class constructor used? A: Its called when object is created. You can initialize class variables. c) When and for what is class destructor used? A: Its called when class is destroyed. You can free dynamically reserved memory. d) What means public in class declaration? A: They are visible and accessible from the outside of the object. e) What means private in class declaration? A: They are not visible or accessible from the outside of the object f) What means protected in class declaration? A: The same as private except they can be inherited
2. a) For what is library iostream needed?
A: Functions cin and cout are in it b) Why is needed line using namespace std; A: To call cout without std::cout. The same for cin c) What is the name of the class? class hehe { }; A: hehe d) What is m? cout << obj.m; A: It is a variable of the object obj e) Is this function declaration or definition? Variable() { variable_v = 0;}; A: Definition f) Is this the default constructor? Variable(int default_value); A: No. It has a parameter. g) What is this? ~Variable() {}; A: Class destructor of class variable h) Is this function declaration or definition? bool setVariable(int new_value); A: Declaration
METROPOLIA Janne Mntykoski
Advanved programming
i) What is the type of function return value and parameter?
bool setVariable(int new_value); A: Return value is boolean (false, true). Parameter is new_value with type int j) What is the class name? Function name? bool Hehe::setVariable(int new_value) { } A: Class name is Hehe and function name is setVariable k) What is the name of the class? Name of the object? Variable obj1(250); A: Class name is Variable and object name is obj1 l) Is default or parameterized constructor used? Variable obj1(250); A: Parameterized constructor m) Is default or parameterized constructor used? Variable obj2; A: Default constructor n) What does this do? cout << "The initial value of obj1 is " << obj1.getValue() << endl; A: It calls the getValue function of object obj1 and prints it return value o) What does this do? cin.get(); A: Will wait for ENTER