Strings
char* s = "ab" will auto terminated with length 3
File input and Output
// == Input ==ifstream inputFile;inputFile.open("Data.txt");inputFile >> inputVariable;// getline, getcharinputFile.close();// == Output ==ofstream outFile;outFile.open("Output.txt");outFile << outputVariable;outFile.close();
Dynamic Memory Allocation
// == Allocate ==ptr = new type;int* iPtr; iPtr = new int;int* iPtr; iPtr = new int[5];// == Delocate ==delete ptr;delete [] ptr;
Classes
class ClassName{private: //only to membersint dataMembers;void myPrivFunctions();protected: // to members and derived functionsdataMembers;MemberFunctions();public: // everywheredataMembers;MemberFunctions();}ClassName::myPrivFunctions(){// statements}int main(){ClassName iVar;ClassName iPrt = &iVar;iVar.member = 3;iVar.MemberFunction();iPrt->member = 3;iPrt->MemberFunction();}
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.
int GetNum() const{;}
Constructors and Destructors
class Rectangle {private:double width;double height;public:Rectangle(){}; //if have below cons, need this since standard constructor is overwritenRectangle(double w, double h){width = w; height = h;}}Rectangle R1(5, 4);R1 = Rectangle(5, 4); // two ways to use itRectangle Recs[100];
Type conversion by constructor
class Fraction{...Fraction(int x){num = x; denom = 1; }}
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;
}
