Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Beginners Lab Assignments Code Examples

Simple Program for Virtual Base Class Using C++ Programming

/* Simple Program for Virtual Base Class Using C++ Programming Step 1: Start the program. Step 2: Declare the base class student. Step 3: Declare and define the functions getnumber() and putnumber(). Step 4: Create the derived class test virtually derived from the base class student. Step 5: Declare and define the function getmarks() and putmarks(). Step 6: Create the derived class sports virtually derived from the base class student. Step 7: Declare and define the function getscore() and putscore(). Step 8: Create the derived class result derived from the class test and sports. Step 9: Declare and define the function display() to calculate the total. Step 10: Create the derived class object obj. Step 11: Call the function get number(),getmarks(),getscore() and display(). Step 12: Stop the program. */ #include<iostream.h> #include<conio.h> class student { int rno; public: void getnumber() { cout << "Enter Roll No:"; cin>>rno; } void putnumber() { cout << "\n\n\tRoll No:" << rno << "\n"; } }; class test : virtual public student { public: int part1, part2; void getmarks() { cout << "Enter Marks\n"; cout << "Part1:"; cin>>part1; cout << "Part2:"; cin>>part2; } void putmarks() { cout << "\tMarks Obtained\n"; cout << "\n\tPart1:" << part1; cout << "\n\tPart2:" << part2; } }; class sports : public virtual student { public: int score; void getscore() { cout << "Enter Sports Score:"; cin>>score; } void putscore() { cout << "\n\tSports Score is:" << score; } }; class result : public test, public sports { int total; public: void display() { total = part1 + part2 + score; putnumber(); putmarks(); putscore(); cout << "\n\tTotal Score:" << total; } }; void main() { result obj; clrscr(); obj.getnumber(); obj.getmarks(); obj.getscore(); obj.display(); getch(); }

A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function. There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function. A 'virtual' is a keyword preceding the normal declaration of a function.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

As we have seen above that when we make the 'call to function' (involved in overriding), the child class function (overriding function) gets called. what if you want to call the C++

Internal method to test if a positive number is prime. Not an efficient algorithm and Internal method to return a 'prime number' at least as large as n. Assumes "n > 0". Insert item x into








C++ Program should have two functions one which takes radius as parameter and second takes diameter as parameter. If enters wrong option then tell users its 'valid selection' and