130 likes | 212 Views
C++ functions can be declared to use default values when none are supplied. Class Circle { Float xc, yc, radius; Circle(float x, float y, float r=1.0 ); … } Example usage: Circle c1(1.0, 1.0); // omit def param; use 1.0 Circle c2(1.0, 1.0, 2.0); // supply “custom” value.
E N D
C++ functions can be declared to use default values when none are supplied Class Circle { Float xc, yc, radius; Circle(float x, float y, float r=1.0); … } Example usage: Circle c1(1.0, 1.0); // omit def param; use 1.0 Circle c2(1.0, 1.0, 2.0); // supply “custom” value CS-1030 Dr. Mark L. Hornick
Review: C++ parameter passing • Passing entire objects by value in C++ is not always desirable • Objects can be big, leading to inefficiency • Copy constructor must be called to make the copy of the object… • Passing by reference or by address eliminates the need to copy entire objects • Only the 4-byte address is copied • Java only passes object references CS-1030 Dr. Mark L. Hornick
Review: parameter passing by object, reference, and address • Review: Here’s a declaration of a method that passes an object by value: • void printName(Employee e); • Usage: printName( anEmp ); • Here’s the modified declaration of the method to tell it to pass the object by reference instead: • void printName(Employee& e); • Usage: printName( anEmp ); // same as above • Here’s the modified declaration of the method to tell it to pass the object by address instead: • void printName(Employee* pe); • Usage: printName( &anEmp ); // address of object CS-1030 Dr. Mark L. Hornick
Passing by reference or address – always advantageous? • Useful if you want a method to be able to modify the variable you are passing to the method • But what if you want to prevent a method from modifying the variable? CS-1030 Dr. Mark L. Hornick
The const specifier • Prevents objects passed by reference or address from being changed within a method • Here’s the modified declaration of the method that passes by reference: • void printName(const Employee& e); • Usage: printName( anEmp ); • Here’s the modified declaration of the method that passes by address: • void printName(const Employee* pe); • Usage: printName( &anEmp ); CS-1030 Dr. Mark L. Hornick
Notation for const and pointers is tricky int x; const int* ptr1 = &x; // Can’t use ptr1 to change x, but can change ptr1 to point to another object int* const ptr2 = &x; // Can’t change ptr2 to another object, but can change value of x const int* const ptr2 = &x; // Can’t do either CS-1030 Dr. Mark L. Hornick
const data objects within classes class Employee { const int ID; Employee(); // def constructor Employee(int id); // constructor … } • Constructor is still called, but const data cannot be initialized in the constructor • So how do you set it? CS-1030 Dr. Mark L. Hornick
const data objects within classes - Initializers class Employee { const int ID; Employee():ID=0; Employee(int id):ID(id); … } • Initializer • Data members are initialized before the constructor is actually invoked CS-1030 Dr. Mark L. Hornick
const Member Functions • May not alter data members of the class • May not call other member functions that modify data members of the class • Example CS-1030 Dr. Mark L. Hornick
static Data Members • Same concept as Java’s static • Belong to all objects of the same type (class) • Shared by all instances • Don’t need an object to exist • Must be defined outside the class • But declared inside • Example CS-1030 Dr. Mark L. Hornick
static Member Functions • Belong to the class • Not the object! • Don’t require an object to be used • May only access static data members • Example CS-1030 Dr. Mark L. Hornick
Who can you trust? • Private members have limited scope • …only methods in the same class can access them • This can be circumvented • any function (from any other class) can be given access to a private data member or method • The outside function must be declared as a friend • must be part of the class definition CS-1030 Dr. Mark L. Hornick
Friends • An entire class can be made a friend of another class • The class must be declared as a friend • The declaration must be part of the class definition • Friendship is not mutual • If class A declares class B as a friend, the friendship is not automatically reciprocated • Class B would have to declare class A as a friend CS-1030 Dr. Mark L. Hornick