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 Hierarchical Inheritance

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* Simple Hierarchical Inheritance Inheritance is when an object or class is based on another object or class, using the same implementation specifying implementation to maintain the same behavior. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. In hierarchical inheritance a single class serves as a superclass (base class) for more than one sub class. */ // Header Files #include<iostream> #include<stdio.h> #include<conio.h> using namespace std; class Person { int eno; char name[20], des[20]; // Private members cannot call from outside class. public: void getPersonDetails() { cout << "\nEnter the Person number:"; cin>>eno; cout << "Enter the Person name:"; cin>>name; cout << "Enter the Person designation:"; cin>>des; } void person_display() { cout <<"\nPerson number:"<<eno; cout <<"\nPerson name:"<<name; cout <<"\nPerson designation:"<<des; } }; class Employee : private Person { //Private Base Class, We cannot access outside the dervied Class float bp, hra, da, pf, np; public: void getEmployeeDetails() { getPersonDetails(); cout << "Enter the Basic pay:"; cin>>bp; cout << "Enter the Humen Resource Allowance:"; cin>>hra; cout << "Enter the Dearness Allowance :"; cin>>da; cout << "Enter the Profitablity Fund:"; cin>>pf; calculate(); } void calculate() { np = bp + hra + da - pf; } void employee_display() { person_display(); cout <<"\nEmployee Basic pay:"<<bp; cout <<"\nEmployee Humen Resource Allowance:"<<hra; cout <<"\nEmployee Dearness Allowance:"<<da; cout <<"\nEmployee Profitablity Fund:"<<pf; cout <<"\nEmployee Net Pay:"<<np; } }; class Student : private Person { char college[20], course[20]; public: void getStudentDetails() { getPersonDetails(); cout << "Enter the Student college Name:"; cin>>college; cout << "Enter the Student course Name:"; cin>>course; } void student_display() { person_display(); cout <<"\nStudent college Name:"<<college; cout <<"\nStudent IFSC:"<<course<<endl; } }; int main() { int i, n; char ch; Student s[10]; Employee e[10]; cout << "Simple Hierarchical Inheritance Example Program : Payroll System \n"; cout << "Enter the number of Student:"; cin>>n; for (i = 0; i < n; i++) { cout << "\nStudent Details # "<<(i+1)<<" : "; s[i].getStudentDetails(); } for (i = 0; i < n; i++) { s[i].student_display(); } cout << "\n\nEnter the number of Employee:"; cin>>n; for (i = 0; i < n; i++) { cout << "\nEmployee Details # "<<(i+1)<<" : "; e[i].getEmployeeDetails(); } for (i = 0; i < n; i++) { e[i].employee_display(); } getch(); return 0; }

#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.

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.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

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.

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class. You can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

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.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

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.

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.

This algorithm takes the input of the number of vertexes. Construct a complete graph for v vertex. So it assigns a color to edges without assigning the same color to 2 adjacent edges.