Hot File

Build C# Multilevel Inheritance

View: 236    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   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.

Introduction

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 236

Comment:

To comment you must be logged in members.

Files with category

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

    View: 370    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

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

    0/0 review
    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++

    View: 376    Download: 0   Comment: 0   Author: none  

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

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

    4.5/1 review
    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#

    View: 410    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

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

    0/1 review
    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

    View: 286    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

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

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

  • Build Pass By Reference To Method In C#

    View: 439    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

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

    0/3 review
    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

    View: 273    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

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

    0/2 review
    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#

    View: 212    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

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

    0/1 review
    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...

  • Build C# Multilevel Inheritance

    View: 236    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

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

    2.5/2 review
    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.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd