1 Lecture 3 More about C++
2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static members –Namespace OperatorsOperators
3 3.5 More about classes
4 Init List Class String } char* chars; int length; public:String(); char* getString(); void setString(char* value); int equals(String *other); } String::String(){ chars = NULL; length = 0; } Constructors order First, the fields are created Second, the object itself is created
5 Init List String::String(): chars(NULL), length(0){} Class String } char* chars; int length; public:String(); char* getString(); void setString(char* value); int equals(String *other); }
6 MACROs in C (reminder) Macro in C –Advantages Global constants Common functionality without function call overhead Common functionality on variety of types –Disadvantage Do not obey scope rules Type-unsafe constructs
7 C++ substitute to MACRO C++ provides substitutes for Macros –Inline functions – to eliminate function call overhead –Const – to substitute MACRO constants –Template functions – for type-safe common functionality on different types (later in this course…)
8 Inline functions class String{ char* chars; int length; public: String() ; String() : chars(NULL), length(0){}; char* getString() {return chars;}; void setString(char* value); int equals(String *other); } inline int max(int a, int b) { return a < b ? b : a; }
9 Const member functions class String{ char* chars; int length; public: ; String() : chars(NULL), length(0){}; constconst const char* getString() const {return chars;}; void setString(char* value); const int equals(const String *other); } Indicates that this function does not modify the state of String The compiler will catch accidental attempts to violate this promise The const is part of the function declaration A const member function can be invoked for both const and non- const objects.
10 Const and functions Const applied to –Arguments void f(const int *i); –Return values const int *f(); this –The this object void C::f() const;
11 Const variables To tell compiler something is constant To limit usage of a variable Example: const float PI = ; –Avoid accidental change if (PI = some_variable) … instead of if (PI == some_variable) Let the compiler optimize
12 Const and pointers p p p const int* p; // int const* p; p++;//ok *p = 17; //error! int* const p; p++; //error! *p = 17; //ok
13 Default function parameters class complex { double re,im; public: complex (double r=0, double i=0) {re = r; im = i;} double real_part() const {return re;} double imag_part() const {return im;} double abs() const {return sqrt(re*re +im*im);} }; int main() { Complex c; Complex c1(2,3); Complex c2(2,0); Complex c3(2); // same as (2,0) Complex c4;// same as (0,0) }
14 Static members class Complex { double r, theta; public: complex (double r=0, double i=0):re(r),im(i){} double real_part() const {return r*cos(theta);} double imag_part() const {return r*sin(theta);} double abs() const {return r;} double arg(double x, double y) const { return !x ? (y >=0 ? PI/2 : -(PI/2)) : atan2(y,x);} }; Since implementation details are hidden it is possible to reimplement the class complex to use polar coordinates
15 Static members Complex::arg()The function Complex::arg() above raises a few points: this –Its action is unrelated to any specific Complex object, yet, as a member function, it has a this z.arg(a,y) z –It’s unreasonable to execute z.arg(a,y) for evaluating an expression that is totally unrelated to z PI PI –We need PI as a data member, why each complex object should have its own copy of PI?
16 Static members (Cont.) class Complex { double r, theta; public: static const doubel PI; complex (double r=0, double i=0): re(r), im(i), PI( ){} double real_part() const {return r*cos(theta);} double imag_part() const {return r*sin(theta);} double abs() const {return r;} static double arg(double x, double y) { return !x ? (y >=0 ? PI/2 : -(PI/2)) : atan2(y,x);} }; Static data membersStatic data members are unique, and belong to the class Static member functionsthisStatic member functions does not have this parameter, hence, can directly access only static data members Static member function scope operatorStatic member function can be called either using an object of the class, or using the scope operator Cannot be const
17 Namespaces Suppose we have two versions of the function sqrt() –One is implemented for speed –The other for accuracy We would like to use in the same project the two versions namespace std { double sqrt (double); double pow (double, double); } namespace alt { double sqrt (double); double pow (double, double); } double pyth(double x, double y) { return std::sqrt(x*x + y*y) }
18 Namespaces (Cont.) Namespace can be nested Namespace can be extended Global access to a selected identifier in a namespace is achieved by a using declaration Global access to all names in a namespace is achieved by a using directive using alt:sqrt; using namespace std;