AlexLecture_ClassInheritance - Header Files Class...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Class Inheritance in C++ Header Files To prevent being included twice # ifndef MY_HEADER # define MY_HEADER //your declarations # endif #include <iostream> : search in “include search path” 2 #include <iostream> : search in include search path Used for system header files #include “MyHeader.h”: search in current directory May check include search path if not found there Used for your own header files Constants #define BUFFERSIZE 100 BUFFERSIZE cannot be used as a variable const int buffersize = 100; const ClassName aConstantObject; What if we invoke an operation on a constant object that can change its data members? aConstantObject.setValue(); Only const member functions can be invoked! int getValue() const {…} //Hi, I will not change data members! const int getValue() {…}//The function’s return value is const. 3 Meaningless for built-in types But meaningful for user-defined types: getStudentRecord().setName() Principle: Query functions (such as getValue()) should always be defined as const. Principle: Passing by reference or by pointer should always be defined as constant if you do not change it. Constant passing by reference is better than passing by value. int compare( const Student& s1, const Student& s2) Note: Constant data members of a class can only be initialized through the member initialization list of the constructor. Student(string name): _studentName(name) {…} Static // In Counter.h class Counter{ public: Counter(){ num++;} static int getNum() { return num; } //Can only refer to static member num private: static unsigned num; //one copy per class string name; //one copy per object } 4 Static class data members are allocated on a per class basis rather than a per object basis. Static class function members can only refer to static data members. }; // In Counter.cpp unsigned Counter::num = 0; // Defines and allocates num Namespace Prevent naming conflicts C++ has a standard string class. But what if IBM also developed a string class?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

AlexLecture_ClassInheritance - Header Files Class...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online