Strings

char* s = "ab" will auto terminated with length 3

File input and Output

  1. // == Input ==
  2. ifstream inputFile;
  3. inputFile.open("Data.txt");
  4. inputFile >> inputVariable;
  5. // getline, getchar
  6. inputFile.close();
  7. // == Output ==
  8. ofstream outFile;
  9. outFile.open("Output.txt");
  10. outFile << outputVariable;
  11. outFile.close();

Dynamic Memory Allocation

  1. // == Allocate ==
  2. ptr = new type;
  3. int* iPtr; iPtr = new int;
  4. int* iPtr; iPtr = new int[5];
  5. // == Delocate ==
  6. delete ptr;
  7. delete [] ptr;

Classes

  1. class ClassName{
  2. private: //only to members
  3. int dataMembers;
  4. void myPrivFunctions();
  5. protected: // to members and derived functions
  6. dataMembers;
  7. MemberFunctions();
  8. public: // everywhere
  9. dataMembers;
  10. MemberFunctions();
  11. }
  12. ClassName::myPrivFunctions(){
  13. // statements
  14. }
  15. int main(){
  16. ClassName iVar;
  17. ClassName iPrt = &iVar;
  18. iVar.member = 3;
  19. iVar.MemberFunction();
  20. iPrt->member = 3;
  21. iPrt->MemberFunction();
  22. }

Scope

  • public: everywehere

  • protected: member functions and derived classes

  • private: members

Constructor and Destructor

Constant Member Function

If a member function does not modify the data members of the support object, it should be declared constant.

  1. int GetNum() const{
  2. ;
  3. }

Constructors and Destructors

  1. class Rectangle {
  2. private:
  3. double width;
  4. double height;
  5. public:
  6. Rectangle(){}; //if have below cons, need this since standard constructor is overwriten
  7. Rectangle(double w, double h){
  8. width = w; height = h;
  9. }
  10. }
  11. Rectangle R1(5, 4);
  12. R1 = Rectangle(5, 4); // two ways to use it
  13. Rectangle Recs[100];

Type conversion by constructor

  1. class Fraction{
  2. ...
  3. Fraction(int x){
  4. num = x; denom = 1; }
  5. }

Copy constructor

class Fraction{
    ...
    Fraction(const Fraction& F){
        num = F.GetNum();
        denum = F.GetDenom();
    }
}// as a constructor, no need to return

Destructor

Every Class has exactly one destructor. If not provided, a default one does nothing. It must be provided with dynamic objects. Memory allocated with new must be released with delete in the destructor.

Operator Overloading

Assignment operator

ClassName & ClassName::operator= (const ClassName & CN){
    xxx = CN.xxx; etc...
    return *this;
}

Polymorphism

It means the same pointer can point to objects of various classes and that appropriate member functions are called through such a pointer. The class of an object accessed by such a pointer is determined at runtime, depending on its actual content. This is called dynamic binding. This is achieved through pointers to base class objects. These pointers are “polymorphic”. It is useful to handling collections of objects.

Static Binding

class Shape
{
     public:
     void Test() {cout << "Shape" << endl;}
};
class Triangle : public Shape
{
     public:
     void Test() {cout << "Triangle" << endl;}
};
    int main()
    {
         Shape S;
         Triangle T;
         S=T; // no pointer used => no polymorphism
         S.Test(); // output: Shape
         system("pause");
}

S is declared as shape, and .Test() will be called as Shape function. It is determined at compilation.

Another wrong trying

int main()
{
     Shape* S; // create pointer to object of class Shape
     Triangle T;
     S = &T; // assign address of T to S
     (*S).Test();  // still is shape
     system("pause");
}

Dynamic binding

Using the key word virtual

class Shape
{
 public:
     virtual void Test() {cout << "Shape" << endl;} // "virtual" makes sure dynamic
};     // binding is used
class Triangle : public Shape
{
 public:
     void Test() {cout << "Triangle" << endl;}
};
int main()
{
     Shape* S;
     Triangle T;
     S = &T;
     (*S).Test(); // output: Triangle
     system("pause");
}

Virtual Functions Rules

  • Say class K and subclass Ksub with both function f

  • Declare f as virtual in K, and use pointer to base class. The version of the class will be determined at runtime.

  • Constructors should be never virtual, but destructors in base should always be virtual

  • only put virtual in the declaration, not implementation

K* ptr; Ksub O; ptr = &O; using ptr...

Pure Virtual Functions

To make a virtual member function a pure virtual function, we put =0 after the function head of the virtual function declaration. With this, the base class becomes abstract, which is, no objects of the class can be created any more (but the objects of non-abstract subclasses, can be created). In this case, implementation of this virtual function should be provided in all subclasses.

The advantage of polymorphism is that it can be used to efficiently manage heterogeneous collection of objects.

File Structures

Modularization

  • Class declaration in K.h, class implementation in K.cpp

  • function collections in function.h

  • the modules of the projects are the .cpp files. Each module will be only recompiled if it has been changed

  • The advantage of header filese: since the source files are independent, and they need to know the shared variables and class are in other source files, it is better to put in a header file, so that it serves as an interface between the source files.

More on Functions and Templates

Pass function as a parameter (by pointer)

void function1(doulbe *(functionPointer) (double)){
     for(int i = 0; i < 4; i++){
        cout << functionPointer(i) << endl;    
    }   
}

double Test(double x){
    return sin(x) +exp(x);
}

int main(){
    function1(Test);
}

Function templates

template <class T1, class T2>
T1 functionName(T1 t1, T2 t2); // declaration

template <class T1, class T2>  // implementation
T1 functionName(T1 t1, T2, t2){
    // do sth        
}

functionName <type1, type2> (parameters); // calling function templates
functionName <double, int> (2.0, 4);    //e.g.

Note that class have nothing to do with the keywork class

Class templates

template <class T1, .. class Tn, type1 par1, typem parm>
class K { ... }

K<type_1, ... type_n, value1, .., value_m> O; // an object of template class. 
// note that the class name is K<type_1, ... type_n, value1, .., value_m>

Note that all the member functions of a class template automatically become function templates and must be implementated using the function template syntax.
All functions that are not member function, but uses objects of a class templates, automatically become template function

Exception Handling

try{
    statement;
}