Build C# Multilevel Inheritance

View: 438    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

File has been tested

C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

If the constructors are defined by all classes in multilevel inheritance the “base” keyword refers to the most nearest constructor in the base/parent class for example there are three classes class A, B and C, we inherit A to B and B to C, the “base” keyword in class C will always refer to the constructor of class B and “base” of class B refers to constructor in class A.

If a constructor in base/parent class are parametrized constructor, then all derived/child classes must have those parameters

General Syntax:

class A
{ }

class B : A
{ }

class C : B
{ }

class D : C
{ }

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // fields are declared with protected access.
        protected double AspMarks;
        protected double CSharpMarks;
 
        // Parametrized constructor which sets marks for each subject.
        public MarkSheet(double aspMarks, double cSharpMarks)
        {
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;
        }
 
        // Double return type method declaration which returns obtain marks.
        public double GetPercentage(double obtainMarks, double totalMarks)
        {
            return (obtainMarks / totalMarks) * 100.0;
        }
    }
 
    // A class BcsMarkSheet which is derived from MarkSheet class.
    class BcsMarkSheet : MarkSheet
    {
        protected double PhpMarks;
 
        // Parametrized constructor which sets marks for each subject in derived class.
        public BcsMarkSheet(double aspMarks, double cSharpMarks, double phpMarks)
            : base(aspMarks, cSharpMarks)
        {
            this.PhpMarks = phpMarks;
        }
 
        // Double return type methods which returns each subject marks.
        public double GetAspMarks()
        {
            return this.AspMarks;
        }
 
        public double GetCsharpMarks()
        {
            return this.CSharpMarks;
        }
 
        public double GetPhpMarks()
        {
            return this.PhpMarks;
        }
 
        public double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;
        }
    }
 
    // A class SoftwareEngMarksheet which is derived from BcsMarkSheet class.
    class SoftwareEngMarksheet : BcsMarkSheet
    {
        double JavaMarks;
 
        // Parametrized constructor which sets marks for each subject.
        public SoftwareEngMarksheet(double aspMarks, double cSharpMarks, double phpMarks, double javaMarks)
            : base(aspMarks, cSharpMarks, phpMarks)
        {
            this.JavaMarks = javaMarks;
        }
 
        public double GetJavaMarks()
        {
            return this.JavaMarks;
        }
 
        new public double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks + this.PhpMarks + this.JavaMarks;
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            // An object creation of SoftwareEngMarksheet class.
            SoftwareEngMarksheet softwareEngMarksheet = new SoftwareEngMarksheet(85, 70, 60, 55);
 
            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1: Asp.net");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetAspMarks());
            Console.WriteLine("Subject 2: Csharp");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetCsharpMarks());
            Console.WriteLine("Subject 3: Php");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetPhpMarks());
            Console.WriteLine("Subject 4: Java");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetJavaMarks());
            Console.WriteLine("Obtain marks: {0}", softwareEngMarksheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", softwareEngMarksheet.GetPercentage(softwareEngMarksheet.GetObtainMarks(), 400));
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        Bcs Mark sheet information:
        Subject 1: Asp.net
        Marks: 85
        Subject 2: Csharp
        Marks: 70
        Subject 3: Php
        Marks: 60
        Subject 4: Java
        Marks: 55
        Obtain marks: 270
        Percentage: 67.5
        
       */
    }
}

Build C# Multilevel Inheritance

C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

Posted on 08-09-2016 

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd