Industrial Training

c++ Tma



Question 1:

  1. What is the purpose of const keyword in C++ ? Describe the const when it is used with function names and / or during parameter passing . Describe with the help examples. How can parameter passing be done using references ? Describe with the help of examples. What are the advantages / disadvantages of const and parameter passing using reference ?

 

  1. A derived class can be derived as public / private and protected . How does these access specifiers relate to access of the class members , interface and objects in the inheritance hierarchy . Describe with the help of examples . What is multiple and multilevel inheritance ? Describe with the help of examples.

Solution :

a)

Const :
Const allow us create types constants . Any value declared as const cannot be modified by the program in any way. These named constants are just like variables whose values can not be changed. C++ requires a const to be initialized. A const in C++ defaults to the internal linkage and therefore it is local to the file where it is declared.

Example :
Const int size = 5;
int a [ size ];

An argument to a function can be declared as const as shown below.
int strlen ( const char * str);

The qualifier const tells the compiler that the function should not modify the argument. The compiler will generate an error when this condition is violated. This type of declaration is significant only when we pass arguments by reference or pointers.

Constant Member Functions
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.
To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant.
Example
// Example of a constant member function
class Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const; // A read-only function
void setMonth( int mn ); // A write function; cannot be const
private:
int month;
};

int Date::getMonth() const
{
return month; // Doesn't modify anything
}
void Date::setMonth( int mn )
{
month = mn; // Modifies data member
}
void main()
{
Date MyDate( 7, 4, 1998 );
const Date BirthDate( 1, 18, 1953 );
MyDate.setMonth( 4 ); // Okay
BirthDate.getMonth(); // Okay
BirthDate.setMonth( 4 ); // Error
}

Value Parameters
If nothing special is written, argument values are copied into corresponding formal parameters, which are like local variables. Assignment to formal parameters or local variables does not change the arguments.
Reference Parameters allow assignment
Reference parameters are used to solve the problem of changing the arguments.
Mark reference parameters with a &
To indicate a reference parameter, an ampersand (&) is written in the function prototype and header after the parameter type name.
Example - Swap (bad solution)
Let's say you want to exchange the values in two arguments.
int a = 5;
int b = 10;

swap(a,b);

// We want a=10 and b=5, how do we write it?
Here's an example that does NOT work correctly, altho there is no error message.
void swap(int x, int y) { // BAD BAD BAD BAD BAD BAD BAD
int temp;
temp = x;
x = y;
y = temp;
}
Because x and y are like local variables, they have no effect on the arguments a and b.
Example - Swap (good solution)
If the parameters are marked as reference parameters, the memory address of each argument is passed to the function. The function can use this address to either get the value or set the value. Here is swap written correctly. The only change is the addition of the & to the parameter types.
void swap(int& x, int& y) {
int temp;
temp = x;
x = y;
y = temp;
}L-values required for actual reference parameters
An l-value is something that you can assign to. This name is short for left-value, referring to the kind of value that must be on the left side of an assignment statement.

 

b)

protected

protected:
[member-list]

protected base-class
The protected keyword specifies access to class members in the member-list up to the next access specifier (public or private) or the end of the class definition. Class members declared as protected can be used only by the following:

  • Member functions of the class that originally declared these members.
  • Friends of the class that originally declared these members.
  • Classes derived with public or protected access from the class that originally declared these members.
  • Direct privately derived classes that also have private access to protected members.

When preceding the name of a base class, the protected keyword specifies that the public and protected members of the base class are protected members of its derived classes.
Protected members are not as private as private members, which are accessible only to members of the class in which they are declared, but they are not as public as public members, which are accessible in any function.
Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.Example
#include <iostream.h>

class X
{
public:
void setProtMemb( int i ) { m_protMemb = i; }
void Display() { cout << m_protMemb << endl; }
protected:
int m_protMemb;
void Protfunc() { cout << "\nAccess allowed\n"; }
} x;

class Y : public X {
public:
void useProtfunc() { Protfunc(); }
} y;

void main()
{
// x.m_protMemb; error, m_protMemb is protected
x.setProtMemb( 0 ); // OK, uses public access function
x.Display();
y.setProtMemb( 5 ); // OK, uses public access function
y.Display();
// x.Protfunc(); error, Protfunc() is protected
y.useProtfunc(); // OK, uses public access function
// in derived class
}

Hi I am Pluto.