C++, as the name indicates is the successor of C. It has many features which builds on the foundation provided by C:
Note that in C++ a class object is created like this:
Box box; //Creates a class object. This is unlike C# where it will just creates a reference.
Box *box = new Box(); //This is also valid for C++. C# doesn't provide pointers.
A class which defines any of these three member functions, should also define the rest of these:
Copy Constructor,
Copy Assignment Operator,
Destructor
These three are automatically created by the compiler if they are not defined in a class. See Rule of Three
Main concepts :
1. Overloaded functions : In C++, more than one function can have the same name. They are differentiated based upon type and no. of parameters passed. This helps in doing slightly different tasks with slightly different parameters. The same task can be also be done, with different parameters. Name mangling is used to deal with problem of same identifier for function names. Linkages are performed using keyword such as extern "C".
2. new and delete operators.
Usage : ptr = new int[4]; delete [] ptr;
3. Reference variables : These are unlike pointers aliases to actual variables. A reference variable is created like this:
int a;
int& b = a;
Note that reference variables are always initialized. They can be used to do pass by reference when calling functions. Pointers can be kept non-initialized but reference should be always initialized.
4. Encapsulation : It's the mechanism by which the interface and behavior of a user-define data type is tied to the implementation and representation. By using encapsulation in object oriented programming, the user defined data types can be viewed in the same way as intrinsic data type.
5. Function Templates : Using function templates the same function template can be used for several types of parameter and return values. It implements the parameterized data types concept.
6. Class : A Class encapsulates data and algorithm that operate on it to form a abstract user-defined data type. Concepts like operator overloading, conversion member functions, default constructors make C++ class same as intrinsic data types.
7.Abstract classes : These classes cannot be instantiased because they contain pure virtual functions.
8.Virtual Functions : Virtual functions are class member functions that can be overridden in derived classes. An abstract class contains atleast one pure virtual function. If the derive class doesn't override the pure virtual function(s), then it also becomes an abstract class. Virtual functions use dynamic binding, to resolve the calls at runtime.
9. Virtual destructors : When an base type containing object of derived type, is freed, the destructor of base class gets called. The destruction of derived class doesn't gets called if the destructor is not declared virtual.
10. Static Member functions and fields : When a field is declared static, inside a class, it can be use without creating an instance of that class. A static member function can be called without specifying any instance.
11. Friend class and friend functions: Friend classes have access to private data members of a class. Likewise friend functions have access to private fields of a class. These functions can be either member functions of other class or function not belonging to any class.
12. STL : STL stands for Standard Template Library which contains serveral classes that can be used for generic data structures implementation like linked lists (list) , map, multimap etc.
13. Copy Constuctore : A copy constructor get called when a new object of the class in which it's defined gets created, and a reference to an existing object of the same class is passed. A copy constructor is defined as follows:
Box::Box(const Box& box)
{
....
....
}
The copy constructor gets called in either of the following ways:
Box Box1;
Box box2 = box1;
Box box2(box1);
14. Conversion Functions : A Conversion function converts between any type and class type. It's defined as follows:
Member Conversion Function : Converts class objects to other types.
operator type(); //where type is the type to convert
For example : operator long();
Class Coversion function : Converts other types to class type.
A class constructor conversion function can be defined as follows:
Box::Box(long a)
View some video tutorials
http://www.deathmonkeyz.com/tutorials
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment