Currently browsing tag

Cplusplus Tutorial

Random number generation in CC

Random number generation in C/C++ Many programs need random number to complete their certain goal. For example, a quiz program needs random number to randomize the question number to be asked first. Likewise a dice game needs random number to select the face when it is thrown. So there are …

New Standard Flowchart of for loop

New Standard Flowchart of for loop. An ISO committee purposed a new Standard Flowchart of for loop. The previous flowchart of for loop is same as that of while loop. The new flowchart is shown below. Here initialization is the initialization of variable like A = 0 or A = …

C Tutorial Class Templates

C++ Tutorial: Class Templates We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example: template class mypair { T values [2]; public: mypair (T first, T second) { values[0]=first; values[1]=second; }}; The class that we have …

C Tutorial Function Templates

C++ Tutorial: Function Templates Function Templates Function templates are used to reduce the repeated code. I want to make you clear about function templates by giving a small example here. Functions abs() and fabs() are used to find the absolute value or non-negative value of a integer and floating point …

C Tutorial Templates

C++ Tutorial: Templates C++ Template is one of the most sophisticated, flexible and powerful feature. It was not the original feature of C++ as it was added later  on. The older versions of compilers do not support this feature.  Templates support generic programming, allowing development of reusable software components with …

C Tutorial Standard Manipulators

C++ Tutorial: Standard Manipulators It is seen that for formatted input/output the stream class ios function are used. To call ios function for formatting, we need to write separate statement and call the function through stream objects cin and cout. Manipulators are the formatting function for input/output that are embedded …

C Tutorial File Handling in C

C++ Tutorial: File Handling in C++ Opening and Closing Files In C++, a file is opened by either of two ways. First way is the constructor function of the file stream class and second way is by making use of member function open() of the same class. After using the …

C Tutorial User Defined Manipulators

C++ Tutorial: User Defined Manipulators In C++, user can define and use manipulator similar to built in manipulators as per the users’ used and desire to control the format of input and output. Similar to the predefined built manipulators use can define non parameterized as well as parameterized manipulators. The …

How to test Stream Errors in C

How to test Stream Errors in C++? The objects cin and cout works well in normal conditions for input and output respectively. However, there can be several abnormal situation in I/O process such as entering string instead of digit, pressing enter key without entering value or some hardware failure. When …

C Tutorial Some Lab Assignments on Virtual Functions

C++ Tutorial: Some Lab Assignments on Virtual Functions Question: Write a program to create a class shape with functions to find area of the shapes and display the name of the shape and other essential component of the class. Create derived classes circle, rectangle and trapezoid each having overridden functions …

C Tutorial Pure Virtual functions and Abstract Class

C++ Tutorial: Pure Virtual functions and Abstract Class Normally, when creating class hierarchy with virtual functions, in most of the cases it seems that the base class pointers are used but the base class objects are rarely created. When the objects of base class are never instantiated, such a class …

C Tutorial Why Virtual Functions are needed

C++ Tutorial: Why Virtual Functions are needed? The following example illustrates this concept of why virtual function is necessary. 1: #include 2: 3: using namespace std; 4: class Animal{ 5: public: 6: void display(){coutdisplay(); 27: 28: panm=&cw; 29: panm->display(); 30: 31: panm=&dg; 32: panm->display(); 33: return 0; 34: } 35: …

C Tutorial Virtual Functions

C++ Tutorial: Virtual Functions Along with function overloading and operator overloading, C++ also has other type of polymorphism called virtual function. In function overloading and operator overloading the selection of the function takes place at compile time while selection of the function for the virtual functions takes place at runtime. …