C++ Programming Code Examples
C++ > Beginners Lab Assignments Code Examples
Simple Multi Level Inheritance
/* Simple Multi Level 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 behaviour. 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 multilevel inheritance a subclass is inherited from another subclass. It is not uncommon that a class is derived from another derived class as shown in the figure "Multilevel Inheritance". Multilevel Inheritance The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class B is known as intermediate base class since it provides a link for the inheritance between A and C. */
// Header Files
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class Employee {
int eno;
char name[20], des[20];
// Private members cannot call from outside class.
public:
void getEmpDetails() {
cout << "\nEnter the Employee number:";
cin>>eno;
cout << "Enter the Employee name:";
cin>>name;
cout << "Enter the Employee designation:";
cin>>des;
}
void employee_display() {
cout <<"\nEmployee number:"<<eno;
cout <<"\nEmployee name:"<<name;
cout <<"\nEmployee designation:"<<des;
}
};
class Salary : private Employee {
//Private Base Class, We cannot access outside the dervied Class
float bp, hra, da, pf, np;
public:
void getPayDetails() {
getEmpDetails();
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 salary_display() {
employee_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 BankCredit : private Salary {
char bank[20], ifsc_code[20];
int account_number;
public:
void getBankDetails() {
getPayDetails();
cout << "Enter the Bank Name:";
cin>>bank;
cout << "Enter the IFSC:";
cin>>ifsc_code;
cout << "Enter the Account Number :";
cin>>account_number;
}
void display() {
salary_display();
cout <<"\nEmployee Bank Name:"<<bank;
cout <<"\nEmployee IFSC:"<<ifsc_code;
cout <<"\nEmployee Account Number:"<<account_number<<endl;
}
};
int main() {
int i, n;
char ch;
BankCredit s[10];
cout << "Simple Multi Level Inheritance Example Program : Payroll System \n";
cout << "Enter the number of employee:";
cin>>n;
for (i = 0; i < n; i++) {
cout << "\nEmployee Details # "<<(i+1)<<" : ";
s[i].getBankDetails();
}
for (i = 0; i < n; i++) {
s[i].display();
}
getch();
return 0;
}
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.
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.
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.
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.
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++.
#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 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.
To swap two numbers in C++, ask to the user to enter the two number, and store both the number in the variable say num1 and num2. Now to swap both the number, first, make a
To calculate "area and perimeter" of a square and rectangle in C++ Programming, you have to ask to the user to enter length and breadth of the rectangle and side length of the square
Get size of the current type. Align depens on the next type and also alignment defined by #pragam pack(#). Offset will be minimum of this size. If the "size of next type" is less than
To find the LCM of 2 or more numbers, make "Multiple of Numbers" and Choose Common multiple. Then take lowest common multiple, lowest common multiple is 'LCM' of numbers.
To reverse a number in C++, then you have to ask to the user to enter a number. Now, start reversing that number to find its reverse and then display its reverse on the screen. Make a