C/C++ Tutorial

  • Submitted by: 
    This source code is written in C++ and it is a simple code. The template functions are actually a bit easier to use than template classes, as the compiler can often deduce the desired type from the function's argument list. C++ templates can be used both for classes and for functions in C++. Sample Code The syntax for declaring a function is similar to that for a class. template < class type...
  • Submitted by: 
    We want to implement array class with the following specifications: - range checking. - array assignment. - arrays that know their size. - compare arrays using '==' and '!=' operators. In the next example we will implement a simple class for integers is called Array, it has the previous specifications, and it has the following structure: class Array { friend ostream & operator << (...
  • Submitted by: 
    Time sunset ( 19 , 0 , 0 ) ; // object of type Time Time & dinnerTime = sunset ; // reference to a Time object Defining a reference to an object is equal to defining an alias to this object. We can use the reference in the left side of an assignment, that means we can assign a new value to this reference and change the original object that is referenced. We can write a public member function...
  • Submitted by: 
    1. Introduction. 2. Basic Principles in Operator Overloading. Introduction: Calling function in some classes may be annoying, especially mathematical classes, for example: a = 2 + 3 assign ( a, add ( 2 , 3 ) ) ; In C++ the overloading principle applies not only to functions, but to operators too. - Operators can be extended to work not just with built-in types but also classes. - Operator is used...
  • Submitted by: 
    Friend Function: Is a function that: 1. is not a member of a class. 2. has access to the class's private and protected members. 3. is considered as a normal external function that is given special access privileges. 4. is not in the class's scope. 5. is not called using the member-selection operators (. and –>) unless they are members of another class. 6. is declared by the class that is...
  • Submitted by: 
    In real-life, complex objects are often built from smaller, simpler objects. You are built from smaller parts: a head, a body, some legs, arms, etc ... This process of building complex objects from simpler ones is called object composition. Composition is used for objects that have/has a relationship to each other. A house has a door, has a roof, has a balcony. C++ allows us to do object...
  • Submitted by: 
    Contents: 1. Constant Variables. 2. Constant Member Functions. Constant Variables: Making variables constant ensures their values are not accidentally changed. Just like the built-in data types (int, double, char, etc…), class objects can be made const by using the const keyword. All const variables must be initialized at time of creation. For example, we define a constant object from Time class...
  • Submitted by: 
    Contents: 1. Destructor. 2. When Constructors and Destructors are Called. Destructor Is a member function in the class, which has the following characteristics: 1. It has the same name as the class preceded with a tilde '~'. For example, the destructor for class Time is declared: ~Time ( ) 2. It doesn't return any type. 3. It is called when objects are destroyed to release all allocated resources...
  • Submitted by: 
    Contents: 1.The Concept of Abstraction. 2.procedural Abstraction and Data Abstraction. 3.Structure in C++. 4.OOP in C++. 5.Time Class Example.
  • Submitted by: 
    Contents: 1. Constructors. 2. Default Constructors. 3. Constructors with Arguments. 4. Time Class with Constructors. Constructors: Constructor is a member function in the class, which has the following characteristics: 1. It has the same name as the class. 2. It doesn't return any type. 3. It is called every time an instance of a class is created. 3. It initialize the class members with initial...

Pages