Build C# Multilevel Inheritance

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

10 point/2 review 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

Build C# Multilevel Inheritance Posted on 08-09-2016  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. 5/10 374

Comment:

To comment you must be logged in members.

Files with category

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 38    Download: 0   Comment: 0

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

    What if you have one or more modules, running independently in a system, which should collaborate between each other? Well, they will have to be able to exchange data, organized as events, messages or packets of some sort.

  • How to Swap Two Numbers using Call by Reference in C

    How to Swap Two Numbers using Call by Reference in C

    View: 551    Download: 0   Comment: 0

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

    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    How to Print Fibonacci Series using Recursion in C, C++

    View: 548    Download: 0   Comment: 0

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

    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    How to Implicitly Typed Arrays In C#

    View: 617    Download: 0   Comment: 0

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

    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    How to C# Static vs Instance Methods As Event Handlers

    View: 423    Download: 0   Comment: 0

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

    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    Build Pass By Reference To Method In C#

    View: 578    Download: 0   Comment: 0

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

    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    How to C# Continue Statement

    View: 395    Download: 0   Comment: 0

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

    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    How to Single Dimensional Arrays In C#

    View: 344    Download: 0   Comment: 0

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

    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

 
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