Speed up development with full-stack environments for every branch.

Learn More

C++ Examples for Initialization Lists and Inheritance

246 Runs 1773 Views 318 Copies
Saved

Saved

tianxiasoft 13

tianxiasoft
published 4 years ago

#include <iostream>

using namespace std;
/******************************************************************************************/
/*  This example shows initialization list of c++  					                  */
/******************************************************************************************/

// a class that represents human information
class HumanData
{
	float		_height;
	int			_age;

public:
	// default constructor
	HumanData() : _height(180), _age(18) { }

	// Display function
	void DisplayData() 
	{
		// output age and height of the human
		cout << "Age : " << _age << endl << "Height : " << _height << endl;
	}
};

// a class that represents student information.
class StudentData : public HumanData
{
protected:
	int			_study_year;
	int			_score;

public:
	// default constructor. It also call the constructor function of parent class( HumanData )
	StudentData() : _study_year(0), _score(0) { }
	
	// Display function
	void DisplayData() 
	{
		// Call the function of parent class.
		HumanData::DisplayData();

		// output study years and score of the student
		cout << "Study years : " << _study_year << endl << "Score : " << _score << endl;
	}
	
};

int main()
{
	// create instance of StudentData
	StudentData st_data;
	// display data of st_data
	st_data.DisplayData();

	// wait for any key pressing
	cin.get();
	return 0;
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).