Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Classes: Headers, Constants, Static Members, Hiding, Pointers & Inheritance - Prof. Xi, Study notes of Computer Science

An overview of various concepts in c++ classes, including header files, constants, static members, information hiding, pointers, and inheritance. Topics covered include the use of header files to prevent duplicate inclusions, defining constants using #define and const, static class members, and the principle of query functions being defined as const. The document also discusses the concept of access specifiers (public, protected, private) and their impact on derived classes, as well as the use of pointers and references with inheritance.

Typology: Study notes

2011/2012

Uploaded on 06/04/2012

proschek
proschek 🇺🇸

3 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download C++ Classes: Headers, Constants, Static Members, Hiding, Pointers & Inheritance - Prof. Xi and more Study notes Computer Science in PDF only on Docsity! 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 ─ 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?  Declare namespace: namespace IbmStringPackage { class string{ //IBM implementation of string class 5 } }  Two ways to use IBM string class: ─ (1) Use IbmStringPackage::string as the class name ─ (2) Declare “using namespace IbmStringPackage;” first, then use string as class name. But in this case, you cannot delare “using namespace std;”. Function Overloading  Same function, but different signatures (return type, parameters)  One type of polymorphism int max( int, int ); double max( double, double ); 6 2 Information Hiding (1/4) class A{ public: //data and functions needed by clients (public interface). protected: //data and functions needed by derived classes. private: //data and function needed by class A only. }; 7  Why private? ─ Client programs do not need them. Only for implementation. ─ They are subject to future change. ─ For private data, client programs may misuse them and cause inconsistency  Why protected? ─ Needed by derived classes. Derived classes are trusted, but client programs are not. Information Hiding (2/4) class DerivedClassName: access_specifier BaseClassName{ //…… }  Access-specifier: {public, protected, private} ─ Default (no access_specifier) = private  Public Inheritance, Protected Inheritance, Private Inheritance  Access control rule: effect = the stricter one of access and base 8 public protected private public public protected private protected protected protected private private X X X Information Hiding (3/4)  When should access be protected or private? ─ In some cases when you want to operate on data with more stringent requirements, you do not want to inherit the entire interface of the base. ─ For example, suppose you have a Dequeue class that contains operations to insert and delete elements at either end of the queue. Now you want to implement a Queue class, which one can only insert on the left and delete on the right. class Dequeue{ public: void insertL( int ); class Queue: private Dequeue{ public: 9 ─ Note: “using” restores insertL() and removeR() public; others remain private. void insertR( int ); int removeL(); int removeR(); protected: //internal data structure }; using Dequeue::insertL; using Dequeue::removeR; }; Information Hiding (4/4)  When access should be protected or private? ─ In some cases when you only want to inherit the implementation of the operations in the base class, and you do not want to inherit any interface at all. ─ For example, suppose you have a Dequeue class that contains operations to insert and delete elements at either end of the queue. Now you want to implement a stack class, which one can only insert on the left and delete on the left. In addition, you want to use new operation names such as push and pop. class Dequeue{ public: class Stack: private Dequeue{ public: void push ( int x ) 10 ─ “Using” must keep the access level the same as the base. void insertL( int ); void insertR( int ); int removeL(); int removeR(); protected: int _left; int _right; }; { insertL(x); }; int pop() {removeL(); }; bool full() { return _left == _right;} protected: using Dequeue::_left; using Dequeue::_right; }; Pointers/references & Inheritance  Pointer (resp. reference) to an object of a derived class vs. pointer (resp. reference) to an object of its base. void foo(){ Manager m, *mPtr; Employee e, *ePtr; 11 … ePtr = &m; // OK mPtr = &e; // Error. An employee may not be a manager. } Advantages of Inheritance  Factor out code that is common in multiple classes ─ Derived class inherits function and data members from base class ─ Derived class may add additional function or data members ─ Derived class may override inherited function members with new methods  Reuse functions that operate on base-class objects 12 ─ Invoke function whose formal parameter is of (reference or pointer) to class C with actual parameter of (reference or pointer to) class derived from C.  Represent domain relationships explicitly in code ─ Manager and employee
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved