Sie sind auf Seite 1von 13

Home Maths Chemistry Physics Medicine Infos

online calculator

partner-pub-2863

FORID:11

ISO-8859-1

Search

1234

Home C++ Inheritance in C++

Inheritance in C++

Inheritance in C++

Inheritance is one of the most important features of object oriented programming. It is the process of creating new classes from an existing class. The existing class is known as the basic class and the newly created class is called as derived class. The derived class inherits all properties of the base class but can add properties of its own. The main advantages of the inheritance are:

It permits code reusability. Once a base class is written and debugged, it can be used in various situations without redefining and rewriting it. Increases the reliability of the code:- Reusing existing code saves time, money and effort and increase programs reliability. Ease of distributing class libraries:- A programmers can use a class created by another person or company and without modifying it, derive other classes from it that are suited to particular situations. Hence, reusability results in the ease of distributing class libraries.

Single inheritance If a class is derived from only one base class, then it is called single inheritance. The general syntax of defining a derived class using single inheritance is given below:Class derived_class_name: visibility_mode base_class_name { :::::::::::::: // member of derived class }; Here, the visibility_mode may be either private or public or protected. If no visibility_mode is specified, then by default the visibility_mode is considered as private. E.g. 1. Develop an object oriented program to create a database of the students information system containing the following information. I. Name II. Roll number III. Address IV. Gender

Design a base class with name, roll number and another derived class consisting of other data members. #include<iostream.h> #include<conio.h> Class student { Private: Char name [25]; Int roll; Public: Void getdata () { Cout<<\n enter name; Cin>>name; Cout<<\n enter roll; Cin>>roll; } Void display () { Cout<<\n Name=<<name; Cout<<\n Roll no=<<roll; } }; Class stu.address: public student

{ Private: Char address [25]; Char gender [25]; Public: Void getdata () { Student:: getdata () Cout<<\n Enter address; Cin>>address; Cout<<\n enter gender; Cin>>gender; } Void display () { Student:: display (); Cout<<\n Address=<<address; Cout<<\n Gender=<<gender; } }; Void main () { Stu.address a;

a.getdata (); a. display (); getch (); } Multiple inheritances in C++

If a class is derived from more than one base class, then it is called multiple inheritances. The general syntax of defining a derived class using multiple inheritances is:Class derived_class_name: visibility_mode base 1, visibility_mode base 2 { // member of derived class }; Difference between single and multiple inheritances In a single inheritance, a derived class has only a single base. Hence, it can inherit from only a class not from more than one base class. In a single inheritance hierarchy, a derived class typically represents a specialization of its base class. But, in multiple inheritances, a derived class has multiple base classes. In a multiple inheritance hierarchy, a derived class typically represents a combination of its base class. Advantages of multiple inheritances over single inheritances In a multiple inheritances, a derived class has multiple base classes whereas in a single inheritance, a derived class has only a single base class. Hence, following are the advantages of multiple inheritances over single inheritance:1. Multiple inheritances can combine the behavior of multiple base classes in a single derived class. 2. It allows combining the information from more than one base class and helps to extend the possibility to reuse the existing classes. 3. It has rich semantics and has the ability of directly express complex structure. Disadvantage of multiple inheritances

1. It increases the complexity and conflicts can arise between inherited attributes and methods. A program to illustrate how a multiple inheritance can be declared and defined in a program. This program consists of two base classes and one derived class. The base class Stuinfo contains the data members: name, roll. And another base class Stuacademic info contains the data members: course and semester. The derived class stuaddressinfo contains the data members: address only. #include<iostream.h> #include<conio.h> Class Stuinfo { Private: Char name [25]; Int roll; Public: Void getdata () { Cout<<\n Enter name; Cin>>name; Cout<<\n Enter roll; Cin>> roll; } Void display () { Cout<<\n name=<<name;

Cout<<\n Roll number<<roll; } }; Class Stuacademicinfo { Private: Char course [25]; Char semester [15]; Public: Void getdata () { Cout<<\n Enter course name; Cin>>course; Cout<<\n Enter semester; Cin>>semester; } Void display () { Cout<<\n Course <<course; Cout<<\n semester<<semester; }; Class stuaddressinfo: public Stuinfo, public Stuacademicinfo {

Private: Char address [25]; Public: Void getdata () { Stuinfo:: getdata (); Stuacademicinfo:: getdata (); Cout<<\n Enter the address; Cin>>address; } Void display () { Stuinfo:: display (); Stuacademic:: display (); Cout <<\n Address<<address; }; Void main () { Stuaddressinfo obj; Obj .getdata (); Obj.dispay (); getch (); }

Suscribe
If you would like to be notified through email ; when new articles are posted here. Then please enter your e-mail address here:
66.249.66.209

Your email:
Enter email ad

Subscribe

Sponsored Links: Navigation.

Chemistry o Atoms and Molecules o Chemical Bonding o Chemical Equilibrium o Chemical Kinetics o Classification of Organic Compound o Electrochemistry o Gaseous State o Ionic Equilibria o Liquid and Solid state o Oxidation-and-reduction o Solution o States of Matter o Surface-Chemistry o Thermodynamics & Thermochemistry chemistry worksheets o Chemical Bonding Worksheet o Chemical Kinetics o Chemical-Equilibrium o Electrochemistry o Gaseous State o General Chemistry Worksheets Atoms, Molecules and Chemical arithmetic o Ionic Equilibria o Liquid and solid state Worksheet o Organic Compounds worksheet o Oxidation and Reduction o Solution o Surface Chemistry o Thermodynamics and Thermochemistry

Computing Technology o Computer System o Evolution of Computers o Input / Output Unit o Logic Function and Boolen Algebra o Number System o Operating System o Programming C++ Infos o Tech Infos Math Formulas Mathematics o Antiderivatives o Basic Trigonometric Formulae o Functions & Graphs. o Limits and Continuity. o Matrices and determinants. o Properties of triangles. o Set Theory. o The Derivative. o Trigonometric Functions & identities Medical Science o Drug Info News o Tech News Physics o Circular Motion o Motion along a straight line. o Newton's laws of motion o Vectors Physics Worksheets

Visual Science
Learn science and mathematics visually! Following is a Newton's cradle , which demonstrates the conservation of energy and momentum. Drag the balls to create a different motion.

We Speak Your Language:


Select Language

2010 science and Technology No part of this website may be reproduced or distributed in any form or by any means without

the prior written permission of the site owner. contact: info [at] sciencego.info Terms Of Service | Privacy Policy Site Designed , Managed and Promoted by Science and Technology

Das könnte Ihnen auch gefallen