Ch 14 C++

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

general format of a friend class

friend ReturnType FunctionName (ParameterTypeList)

default copy constructor

if a class doesn't have a copy constructor it creates a default copy constructor for it; performs the member wise assignment

you declare a static member function by placing the static key word in the function's prototype; general form:

static ReturnType FunctionName(ParameterTypeList);

A function that is a static member of a class cannot access any nonstatic member data in its class. With this limitation in mind, you might wonder what purpose static member functions serve. The following two points are important for understanding their usefulness:

- Even though static member variables are declared in a class, they are actually defined outside the class declaration. The lifetime of a class's static member variable is the lifetime of the program. This means that a class's static member variables come into existence before any instances of the class are created. - A class's static member functions can be called before any instances of the class are created. This means that a class's static member functions can access the class's static member variables before any instances of the class are defined in memory. This gives you the ability to create very specialized setup routines for class objects.

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

A copy constructor has a reference parameter of the same class type as the constructor's class.

Write a full class definition for a class named Counter, and containing the following members: - A data member counter of type int. - A data member named limit of type int. - A static int data member named nCounters which is initialized to 0. - A constructor that takes two int arguments and assigns the first one to counter and the second one to limit. It also adds one to the static variable nCounters - A member function called increment that accepts no parameters and returns no value. If the data member counter is less than limit, increment just adds one to the instance variable counter. - A member function called decrement that accepts no parameters and returns no value. If counter is greater than zero, decrement subtracts one from the counter. - A member function called getValue that accepts no parameters. It returns the value of the instance variable counter. - A static function named getNCounters that accepts no parameters and return an int. getNCounters returns the value of the static variable nCounters.

Class Counter{ ```private: ````int counter, limit; ````static int nCounters; ```public: ````Counter(int, int); ````void increment(); ````void decrement(); ````int getValue(); ````static int getNCounters(); }; int Counter::nCounters=0; Counter::Counter (int c, int l):counter(c),limit(l) {nCounters++;} void Counter::increment() {if (counter<limit) counter++;} void Counter::decrement() {if (counter>0) counter--;} int Counter::getValue() {return counter;} int Counter::getNcounters() {return nCounters;}

Assume that a class named Numbers has the following static member function declaration: static void showTotal(); Write a statement that calls the showTotal function

Numbers::showTotal();

copy constructor

StudentTestScores(StudentTestScores &obj) { studentName = obj.studentName; numTestScores = obj.numTestScores; testScores = new double[numTestScores]; for (int i = 0; i < length; i++) testScores[i] = obj.testScores[i]; }

the this function

a special built-in pointer that is automatically passed as a hidden argument to all nonstatic member functions.

add member function

adds what ever is in the () to the variable: today.add(5); the += operator must be overloaded for this action to occur

static

all instances of that class have access to that variable; it may be called without any instances of the class being defined

When does a static member variable come into existence in memory?

before any instance of the class

a friend is a function or class that is not a member of a class

but has access to the private members of the class; is treated as if it were a member of the class; can be a regular stand alone function or it can be a member of the other class; an entire class can be declared a friend of another class

When a function is declared a friend by a class, it becomes a member of that class.

false

aggregation

occurs when a class contains an instance of another class

The = operator may be used to assign one object's data to another object

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.

const key word ensures that the function cannot change the contents of the parameter

prevents you from inadvertently writing code that corrupts data

the following statement performs a member wise assignment

student2 = student1;

forward declaration : class Budget;//forward declaration of budget class

tells the compiler that a class named budget will be declared later in the program

if you wish to redefine the way a particular operator works with an object you define a function for that operator

the operator function is then executed anytime the operator is used with an object of that class

void operator = (const studentTestScores &right)

void - return type operator - function name const studentTestScores &right - parameter for object on the right side of the operator

A copy constructor is a special constructor that is called

whenever a new object is created and initialized with another object's data.


Ensembles d'études connexes

Hematopoiesis and Thrombopoiesis

View Set

Incentives, Entreprenuership, and Types of Businesses

View Set

Personality Psychology Chapter 3

View Set

World Geography Introduction Midterm exam

View Set

Final Exam Your Money and credit Uark

View Set