CH 11

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Problem with overriding?

If a derived class overrides a base class member function, member functions of the derived class that would have otherwise called the overridden base class member function will now call the version in the derived class.

How do you create a static member variable?

By placing the key word static in front of the declaration and placing the definition outside of class.

Define what a default copy onstructor is.

If the programmer does not specify a copy constructor for the class, then the compiler automatically calls a default copy constructor. This default copy constructor simply copies the data of the existing object to the new object using memberwise assignment.

Difference between overloading and overriding?

Overriding can only be done in the context of inheritance and refers to the defining of a member function by a derived class when the base class already has a member function of the same name and parameter list. Overloading refers to the definition of different functions within the same class with the same name and different parameter lists.

Do parameters to operator functions need to be passed by reference?

Parameters to operator functions do not have to be passed by reference, nor do they have to be declared const.

The general form for a static member function?

static <return type><function name (<parameter list>)

How could you rewrite: this->x

(*this).x

Which operators cannot be overloaded?

?: . .* :: sizeof

Define copy constructor

A copy constructor is a special constructor that is called whenever a new object is created and initialized with the data of another object of the same class.

What does it entail to override a base class function?

A derived class can override a member function of its base class by defining a derived class member function with the same name and parameter list.

How is a programmer-defined copy constructor built. Why use it instead of the default?

A programmer-defined copy constructor must have a single parameter that is a reference to the same class. This copy constructor avoids the problems of the default copy constructor by allocating separate memory for the pointer of the new object before doing the copy

What does it mean if a member variable is declared static?

All objects of that class have access to that variable

How to gain access to an Overridden Member Function? Let's say Der has a function overriding the function Base.

Base::func();

Why is it a good idea to make the reference parameter constant? modify the copy constructor has a constant ref parameter: NumberArray(NumberArray &obj)

Because copy constructors are required to use reference parameters, they have access to their argument's data. Since the purpose of a copy constructor is to make a copy of the argument, there is no reason the constructor should modify the argument's data. NumberArray(const NumberArray &obj)

Why does it make sense to think of a base class as a superclass of its derived class?

Because derived class objects can be considered as forming a subset of the set of base class objects. Hence we can think of the base class as a "uperset" or superclass of the derived class.

Length a(4, 2), c(0); Why would: "c = a + 2;" compile but not "c = 2 + a;" ? What could be done to fix this error?

Because the operator function is declared as a member function making the left parameter implicit so the compiler interprets c = 2 + a; as c = 2.operator+(a); Declaring the operator function as a stand-alone function (making it a friend)

Why should you make the copy constructors paremeter constant?

Because the purpose of a copy constructor is to copy the argument and not alter the arguments data. The parameter in a copy constructor is passed by reference which may alter the arguments data

Does a static member variable come into existence in memory before, at the same time as, or after any instances of its class?

Before

How does the postfix function look? What are the two differences between prefix overloading and postfix overloading?

Class Class::operator++(int) { Class temp = *this; member_variable++; return temp; } dummy parameter to indicate to the compiler that the overloading is in postfix-mode. use of a temporary local variable 'temp' to capture the value of the object before it is incremented. It is later returned by the funtion.

Define: Class aggregation Class composition

Class aggregation occurs when an object of one class owns an object of another class. Class composition is a form of aggregation where the owner class controls the lifetime of objects of the owned class.

Give the prototype for the type conversion operator function. (use the conversion to a double as an example).

Class::operator double() const { return // return the conversion }

Define convert constructor.

Convert constructors convert a value of a given type to an object of the class

Why do you think the arguments to a base class constructor are specified in the definition of the derived class constructor rather than in the declaration?

Declarations are for typechecking, definitions are for code generation. The compiler needs the arguments to the base class constructor when it is generating code.

What is the reason that base class constructors are called before derived class constructors?

Derived class constructors can assume members of the base class object have already been initialized

What is the difference between an instance member variable and a static member variable?

Each class object (an instance of a class) has its own copy of the class's instance member variables. If a class's member variable is static, however, only one copy of the variable exists in memory. All objects of that class have access to that one variable.

Modify the constructor: Faculty(string fname, Discipline d) { name = fname; department = d; } so that it calls the base class Person's constructor (Person(fname) when a derived class object is called.

Faculty(string fname, Discipline d):Person(fname) { department = d; }

A function with a constant parameter x can turn around and pass x as a non-constant parameter to another function: True/False

False

What could cause a compiler error regarding copy constructors?

Forgetting & that defines referenceparameters

Why does the copy constructor have to be passed by reference?

In case an object is being passed by value in a function call.

What is a base class and a derived class?

It is the two classes that are involved in inheritance. The base class is the general class and the derived class is the specialized class. The derived class is based on, or derived from, the base class.

What are the two ways you can overload an operator?

Make the overloaded operator a member function of the class. This allows the operator function access to private members of the class. It also allows the function to use the implicit this pointer parameter to access the calling object. 2. Make the overloaded member function a separate, stand-alone function. When overloaded in this manner, the operator function must be declared a friend of the class to have access to the private members of the class. Some operators, such as the stream input and output operators >> and <<, must be overloaded as stand-alone functions.

What is the difference between member access specification and base class access specification?

Member access specification determines how a class member is accessible to code outside of the class. Base class access specification determines how members inherited from a base class will be accessed through the derived class.

How would you define the copy constructor for the NumberArray class?

NumberArray::NumberArray(NumberArray &obj)

Define: Protected members

Protected members of a base class are like private members, except they may be accessed by derived classes.

What limitation does a static member function have?

Static member functions cannot access instance members unless they explicitly specify an object of the class

What is memberwise assignment?

The = operator may be used to assign one object to another, or to initialize one object with another object's data. By default, each member of one object is copied to its counterpart in the other object.

Define base class access.

The base class access specification determines how members inherited from the base class will be accessed in the derived class.

How is the derived class set up?

The derived class inherits the member variables and member functions of the base class without any of them being rewritten. Furthermore, new member variables and functions may be added to the derived class to make it more specialized than the base class.

How does the compiler know that a member function is a copy constructor?

The member function has the same name as the class, has no return type, and has a single reference parameter to the same type as the class.

Why does the first objects data change when we change the second objects data?

The reason changing the data in one object changes the other object is that the memberwise assignment performed by the default copy constructor copies the value of the pointer in the first object to the pointer in the second object, leaving both pointers pointing to the same data.

Describe a situation in which memberwise assignment should not be used.

When an object contains a pointer to dynamically allocated memory

What is the relation between baseclasses and derived classes and construction/destruction?

When an object of a derived class is being instantiated, the base class constructor is called before the derived class constructor. When the object is destroyed, the derived class destructor is called before the base class destructor.

What is a has-a relation? What is a is-a relation?

When one class contains an instance of a second class, the first class is said to sustain a has-a relation to the second. When one object is a specialized version of another object, there is an is-a relationship between them.

Name times when default constructors behaviour is not what we expect.

When the data of the first object is changed when we only want to change the second objects data

What is multiple inheritance and should it be used?

a derived class simultaneously derives from two or more base classes. can lead to programs that are very difficult to understand, and is rarely useful in practice.

How does class aggregation occur?

as result of class A having a member of type B, but it can also occur as result of A having a pointer to an object of B.

What is "++b" a user-friendly version of?

b.operator++();

Prototype for: base class derived class

class Base { private: //base mem var public: //base mem func } class Der: public Base { private: //new vars public: //base mem funcs + new funcs }

When should a assignment operator function be used?

classes allocating dynamic memory to a pointer member in any constructor should define both a copy constructor and an overloaded assignment operator.

Define default copy constructor Explain defificiences with the default copy constructor.

default copy constructor simply copies the data of the existing object to the new object using memberwise assignment. If you initialize a second object with the data of the first, and then change the array in the second object. The data in the first will also be altered which is undesirable.

What is one way to overload the assignment operator?

define an operator function called operator= as a member function of the class.

General format of a friend declaration? Where is it placed?

friend <return type><function name>(<parameter type list>); must be declared as a friend by the class granting it access.

Define 'this'

implicit parameter provided by compiler to each member function of a class that points to the object through which the member function is called

What is used to establish a is-a relationship?

inheritance

Define a friend

is a function/class that is not a member of a class, but has access to the private members of the class.

Define operator overloading

it is when you modify the behavior of the assignment operator so that it does something other than memberwise assignment when it is applied to objects of classes that have pointer members.

What does it indicate if there is a is-a relation?

it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special

Define: member initialization list Give the structure/placement.

list is a list of comma-separated calls to member object constructors It is prefixed with a colon and is placed just after the header, but before the body, of the constructor of the containing class:

Display the differences between the stream output operator and the stream insertion operator in regards to overloading.

ostream &operator<<(ostream& out, Class a) { out << a.memFunc(); return out; } istream &operator>>(istream &in, Class &a) { int var; cout << "Enter var: "; in >> var; a.memFunc(var); return in; }

What should the assignment operators return value be? Why?

return *this In order to return the value of the left operand aswell.

Define superclass and subclass.

subclass=derived class superclass=base class

What does it mean if a member function is declared static?

the function may be called before any instances of the class are defined

How does the implementation of the operator function look like?

void class::operator=(const class &obj) { if (arraySize > 0) delete [] aPtr; arraySize = obj.arraySize; aPtr = new double[arraySize]; for (int index = 0; index < arraySize; index++) aPtr[index] = obj.aPtr[index]; } The function starts out by deleting memory allocated to pointers in the object being assigned to, then makes a copy of the other object in pretty much the same way as the copy constructor for the class.

What is the prototype for the operator= function?

void operator=(const class &obj)

List the times a copy constructor is automatically called.

when: • A variable is being initialized from an object of the same class • A function is called with a value parameter of the class • A function is returning a value that is an object of the class

When are copy constructors automatically invoked?(4 points)

whenever the context demands a class object but a value of constructor's parameter type is provided: 1.An object of the class is initialized with a value of the convert constructor's parameter type 2. An object of the class is assigned a value of the convert constructor's parameter type 3. A function expecting a value parameter of the class type is instead passed a value of the constructor's parameter type. 4. A function that declares a return value of the class type actually returns a value of the convert constructor's parameter type.

List some general issues with operator overloading?

you can change an operator's entire meaning, if that's what you wish to do. you cannot change the number of operands taken by an operator. The = symbol must always be a binary operator. Likewise, ++ and -- must always be unary operators. you may overload most of the C++ operators, you cannot overload all of them.

When are the 3 instances a copy constructor is called?

• A variable is being initialized from an object of the same class • A function is called with a value parameter of the class • A function is returning a value that is an object of the class


Ensembles d'études connexes

MARK Chapter 15 and Short Answers

View Set

Independent, Dependent and Controlled Variables

View Set

Community - Women's Health Promotion missed questions

View Set

Section E: East India Company under Lord Cornwallis

View Set