constructor in inherited class c#
C#
// C# program to illustrate the
// concept of inheritance in the
// constructor when the derived
// class contains a constructor
using System;
// Class Tank to give the
// dimension of the tank
class Tank {
double t_radius;
double t_height;
// Properties for Radius and Height
public double Radius
{
get {
return t_radius;
}
set {
t_radius = value < 0 ? -value : value;
}
}
public double Height
{
get {
return t_height;
}
set {
t_height = value < 0 ? -value : value;
}
}
// Display the dimension of tanks
public void DisplayDimension()
{
Console.WriteLine("The radius of tank is :" + Radius
+ " and the height of tank is :" + Height);
}
}
// A derived class AreaOfTank
// inheriting Tank Class
class AreaOfTank : Tank {
string Color;
// Constructor
public AreaOfTank(string c, double r, double h)
{
// from base class
Radius = r;
Height = h;
// from derived class
Color = c;
}
// Return area of tank
public double Area()
{
return 2 * 3.14 * Radius * Height;
}
// Display the color of tank
public void DisplayColor()
{
Console.WriteLine("The Color of tank is "
+ Color);
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
// Create and initialize the
// object of AreaOfTank
AreaOfTank t1 = new AreaOfTank("Green", 6.0, 12.0);
t1.DisplayColor();
t1.DisplayDimension();
Console.WriteLine("Area is " + t1.Area());
}
}// C# program to illustrate the concept of
// inheritance in constructors when both
// the base class and derived class
// their own constructors
using System;
// Class Tank to give the
// dimension of the tank
class Tank {
double t_radius;
double t_height;
// Constructor for Tank
public Tank(double r, double h)
{
Radius = r;
Height = h;
}
// Properties for Radius
// and Height
public double Radius
{
get {
return t_radius;
}
set {
t_radius = value < 0 ? -value : value;
}
}
public double Height
{
get {
return t_height;
}
set {
t_height = value < 0 ? -value : value;
}
}
// Display the dimension of tanks
public void DisplayDimension()
{
Console.WriteLine("The radius of tank is :" + Radius
+ " and the height of tank is :" + Height);
}
}
// AreaOfTank is derived class
// which is inheriting the Tank class
class AreaOfTank : Tank {
string Color;
// Call the Constructor of the
// base class, i.e Tank
// Using base keyword
public AreaOfTank(string c, double r,
double h) : base(r, h)
{
Color = c;
}
// Return area of tank
public double Area()
{
return 2 * 3.14 * Radius * Height;
}
// Display the color of tank
public void DisplayColor()
{
Console.WriteLine("The Color of tank is " + Color);
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
// Create and initialize the
// object of AreaOfTank
AreaOfTank t1 = new AreaOfTank("Brown", 4.0, 8.0);
t1.DisplayColor();
t1.DisplayDimension();
Console.WriteLine("Area is " + t1.Area());
}
}
Also in C#:
- Title
- c sharp string replace
- Category
- C#
- Title
- set textbox colour to transparent c#
- Category
- C#
- Title
- button color uwp c#
- Category
- C#
- Title
- wpf binding object get value
- Category
- C#
- Title
- verifyusertokenasync password reset token
- Category
- C#
- Title
- Read csv file into wpf C#
- Category
- C#
- Title
- c# length 2d array
- Category
- C#
- Title
- bulk update in c# using jquery datatble
- Category
- C#
- Title
- c# skip following code in loop
- Category
- C#
- Title
- asp.net mvc return file
- Category
- C#
- Title
- check if current time is in the morning c#
- Category
- C#
- Title
- move to where it facing unity 2d
- Category
- C#
- Title
- Check object is in layermask unity
- Category
- C#
- Title
- eager loading c#
- Category
- C#
- Title
- parsing string to int without format exception c#
- Category
- C#
- Title
- windows forms iterate through all controls
- Category
- C#
- Title
- mouseposition unity
- Category
- C#
- Title
- how to get specific length of row in matrix c#
- Category
- C#
- Title
- Request.ServerVariables["HTTP_X_FORWARDED_FOR"] get only one ipaddress
- Category
- C#
- Title
- using mediamanager how to play mp3 files
- Category
- C#
- Title
- get first and last item list c#
- Category
- C#
- Title
- as c#
- Category
- C#
- Title
- c# class to byte array
- Category
- C#
- Title
- ASP select box all states
- Category
- C#
- Title
- text variable type unity
- Category
- C#
- Title
- unity get component
- Category
- C#
- Title
- csharp check if env is development
- Category
- C#
- Title
- parse strings into words C#
- Category
- C#
- Title
- read in multiple numbers c#
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- c# filter non alphanumeric characters
- Category
- C#
- Title
- What is a class in c#
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- switch case c# range
- Category
- C#
- Title
- attribute usage c#
- Category
- C#
- Title
- bytes size c#
- Category
- C#
- Title
- clear combobox c#
- Category
- C#
- Title
- c# AllowSynchronousIO to true
- Category
- C#
- Title
- unity detect any key
- Category
- C#
- Title
- c# variable
- Category
- C#
- Title
- external font family uwp c#
- Category
- C#
- Title
- how to make a float in C++
- Category
- C#
- Title
- uwp file open picker
- Category
- C#
- Title
- c# region tag
- Category
- C#
- Title
- how to get joypad axis input unity
- Category
- C#
- Title
- c# change label forecolor code
- Category
- C#
- Title
- Could not load file or assembly 'Ubiety.Dns.Core, Version=2.2.1.0
- Category
- C#
- Title
- blazor display validation message
- Category
- C#
- Title
- create a file in the directory of the exe and write to it c#
- Category
- C#
- Title
- set decimal point c#
- Category
- C#
- Title
- c# format string to date yyyymmdd
- Category
- C#
- Title
- c# string array
- Category
- C#
- Title
- c# making a folder wpf
- Category
- C#
- Title
- loop over enum values
- Category
- C#
- Title
- how to stream video from vlc in c#
- Category
- C#
- Title
- xamarin forms alarm
- Category
- C#
- Title
- unity rotation
- Category
- C#
- Title
- converting letter to ascii
- Category
- C#
- Title
- unity rotate object relative to camera
- Category
- C#
- Title
- string to uint c#
- Category
- C#
- Title
- unity material offset script
- Category
- C#
- Title
- print content of array c#
- Category
- C#
- Title
- unity instantiate vector3
- Category
- C#
- Title
- c# escape characters
- Category
- C#
- Title
- unity t-flip flop
- Category
- C#
- Title
- make camera follow character unity 2020
- Category
- C#
- Title
- rotation unity script 2d
- Category
- C#
- Title
- C# move form without border
- Category
- C#
- Title
- how to make infinite loop in c#
- Category
- C#
- Title
- remove items from list c# condition
- Category
- C#
- Title
- list clone - C#
- Category
- C#
- Title
- iteration c#
- Category
- C#
- Title
- c# integer to bit string
- Category
- C#
- Title
- c# sort for loop
- Category
- C#
- Title
- try catch c#
- Category
- C#
- Title
- .net loop through dictionary
- Category
- C#
- Title
- how to destroy a gameobject after some hits in unity 3d
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- what data type should be for contact number in asp.net
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- C# function return datareader
- Category
- C#
- Title
- .sh script: check if file exist
- Category
- C#
- Title
- cc# sort list with list if ids
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- c# empty IEnumerable
- Category
- C#
- Title
- membership get user id
- Category
- C#
- Title
- how to create public variable in c#
- Category
- C#
- Title
- c# object list attribute to string
- Category
- C#
- Title
- make string
- Category
- C#
- Title
- how to copy one array value to another without reference c#
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- unity delete specific text in a string
- Category
- C#
- Title
- c# inotifypropertychanged best practices
- Category
- C#
- Title
- c# check if string is only letters and numbers
- Category
- C#
- Title
- load information with txt file to uwp c#
- Category
- C#
- Title
- c# udpclient receive buffer size
- Category
- C#
- Title
- c# is not equal to
- Category
- C#
- Title
- how to reference a child object unity
- Category
- C#
- Title
- how do I attach a player with a navMeshAgent
- Category
- C#
- Title
- get setter c# model
- Category
- C#