c# inheritance constructor
C#
// 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());
}
}
// 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());
}
}
Also in C#:
- Title
- how to convert string to bool c#
- Category
- C#
- Title
- what is a return statement C#
- Category
- C#
- Title
- docker ssh
- Category
- C#
- Title
- c# dictionaries
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- bool toggle unity c#
- Category
- C#
- Title
- c# mathf.ceiling
- Category
- C#
- Title
- vb.net check if datatable has rows
- Category
- C#
- Title
- unity cast float to int
- Category
- C#
- Title
- c sharp list indexer
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- c# remove from list in foreach
- Category
- C#
- Title
- perlin noise unity
- Category
- C#
- Title
- bubble sort c#
- Category
- C#
- Title
- .net core session
- Category
- C#
- Title
- decimals not stored in azure tables
- Category
- C#
- Title
- degree to radians c#
- Category
- C#
- Title
- c# get current directory xamarin
- Category
- C#
- Title
- delegate function c#
- Category
- C#
- Title
- asp.net tag helper checkbox
- Category
- C#
- Title
- c# enum to int array
- Category
- C#
- Title
- c# replace foreach with lambda
- Category
- C#
- Title
- c# check if string is in array
- Category
- C#
- Title
- show double in textbox c#
- Category
- C#
- Title
- add row count devepxress report
- Category
- C#
- Title
- idbset sqlquery
- Category
- C#
- Title
- c# download string url
- Category
- C#
- Title
- c# instantiate
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- c# calculate difference between two dates in days
- Category
- C#
- Title
- how to make if statement c#
- Category
- C#
- Title
- c# write variable in string
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- c# round number up
- Category
- C#
- Title
- how to get joypad axis input unity
- Category
- C#
- Title
- unity find object by name
- Category
- C#
- Title
- C# invoke
- Category
- C#
- Title
- c sharp add item to dictionary
- Category
- C#
- Title
- c# check to see if dictionary key exists
- Category
- C#
- Title
- parsing string to int c#
- Category
- C#
- Title
- c# float to string
- Category
- C#
- Title
- south african id number validation c#
- Category
- C#
- Title
- C# get all child classes of a class
- Category
- C#
- Title
- html hidden text
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- how to chagne rotation in unity
- Category
- C#
- Title
- c# math to radiant
- Category
- C#
- Title
- unity how to see what scen you are in
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- ubuntu: how to open the terminal from c#
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- how to get the last element in an array in c#
- Category
- C#
- Title
- c# find index element array
- Category
- C#
- Title
- c# download file
- Category
- C#
- Title
- unity onselect gizmos wireframe
- Category
- C#
- Title
- unity get data from firebase
- Category
- C#
- Title
- unity deactive all object in list
- Category
- C#
- Title
- c# scene manager
- Category
- C#
- Title
- unity cast int to float
- Category
- C#
- Title
- unity how to tell when a gameobject is colliding
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- How to get number of months between 2 dates c#
- Category
- C#
- Title
- how to see if they are aholding down a key unity
- Category
- C#
- Title
- unity detect number key
- Category
- C#
- Title
- how to join array indexes with comma in c#
- Category
- C#
- Title
- Unity C# add slider into editor
- Category
- C#
- Title
- get query string parameter from string value c#
- Category
- C#
- Title
- leantween id
- Category
- C#
- Title
- ASP select box all states
- Category
- C#
- Title
- what does gameobject.find return
- Category
- C#
- Title
- C# get all files in directory
- Category
- C#
- Title
- list of vectors c#
- Category
- C#
- Title
- add mime type for woff in web.config
- Category
- C#
- Title
- c# two dimensional array
- Category
- C#
- Title
- unity remove parent
- Category
- C#
- Title
- autoresetevent
- Category
- C#
- Title
- wpf c# select folder path
- Category
- C#
- Title
- C# enum
- Category
- C#
- Title
- how to remove space between string in c#
- Category
- C#
- Title
- windows forms iterate through all controls
- Category
- C#
- Title
- read configuration workerservice
- Category
- C#
- Title
- c# inline initialize dictionary
- Category
- C#
- Title
- How to search for a string from readline in c#
- Category
- C#
- Title
- c# dictionary add
- Category
- C#
- Title
- unity how to get y value
- Category
- C#
- Title
- c list add element
- Category
- C#
- Title
- unity access child
- Category
- C#
- Title
- entity framework core
- Category
- C#
- Title
- IEnumerator
- Category
- C#
- Title
- get string character by index c#
- Category
- C#
- Title
- Linq - Random Elements
- Category
- C#
- Title
- c# bitmap to array byte
- Category
- C#
- Title
- C# datareadeer return null
- Category
- C#
- Title
- .net core get image from url
- Category
- C#
- Title
- c# how to refresh your binding source
- Category
- C#
- Title
- how to make an array in csharp
- Category
- C#
- Title
- change textbox location C#
- Category
- C#
- Title
- linq in c#
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- convert double to currency c#
- Category
- C#