Loading...

Build C# Multilevel Inheritance

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

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

  • Sound Player in Visual Studio C++ .NET

    Sound Player in Visual Studio C++ .NET

    View: 0    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

  • Quiz Management System in C++

    Quiz Management System in C++

    View: 5    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

  • General Quiz

    General Quiz

    View: 77    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is one of my first WPF projects, and it may be used as a template for a quiz test. The code can be improved but I think its useful for the ones who don't have experience with WPF.

  • Calculating Average and Grade

    Calculating Average and Grade

    View: 25    Download: 2   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Calculating Average and Grade is a small program in order to find the result of students in the class.

  • H.K.M.S SUPREME TYRES

    H.K.M.S SUPREME TYRES

    View: 12    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    H.K.M.S SUPREME TYRES --------------------- Goals: --------- * Brand Master * Size Master * User Master * Customer Master * Purchase Form * Order Form * Bill Form Software is as follows: ------------------------------ * Microsoft Visual Studio 2013 *...

  • Cronpp - a C++ library for CRON expressions

    Cronpp - a C++ library for CRON expressions

    View: 12    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    A CRON expression is a string composed of six fields (in some implementation seven), separated by a whites space, representing a time schedule. CRON expressions are used in various job schedulers (such as the Linux job scheduler, the Quartz...

  • Departmental store management system

    Departmental store management system

    View: 19    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is a simple project on departmental store management system developed in c++ programming language . This project helps to keep all the information details of the items available in departmental store and maintains the stock and prints the bill....

  • Volume of a Cone in C

    Volume of a Cone in C

    View: 23    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Code Features: 1. Find the Volume of a cone 2. Find the value of other properties of the cone 3. Teach user how to compute the volume of the cone manually Thanks to following for the code: John Carlo C. Nañoz, Dondon Bagasbas, Gabby Peñones

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