Notes
Notes - notes.io |
class Shape
{
public double Width;
public double Height;
public void ShowDim()
{
Console.WriteLine("Width and height are " +
Width + " and " + Height);
}
}
// Triangle is derived from Shape.
//Drived class or Child class.
class Triangle : Shape
{
public string Style; // style of triangle
// Return area of triangle.
public double Area()
{
return Width * Height / 2;
}
// Display a triangle's style.
public void ShowStyle()
{
Console.WriteLine("Triangle is " + Style);
}
}
//Driver class which runs the program.
class Driver
{
static void Main()
{
Triangle t1 new Triangle();
Triangle t2 new Triangle();
t1.Width =4.0;
t1.Height =4.0;
t1.Style ="isosceles";
t2.Width =8.0;
t2.Height =12.0;
t2.Style ="right";
Console.WriteLine("Info for t1: ");
t1.ShowStyle();
t1.ShowDim();
Console.WriteLine("Area is " + t1.Area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.ShowStyle();
t2.ShowDim();
Console.WriteLine("Area is " + t2.Area());
}
}
// C# program to illustrate the
// concept of multilevel inheritance
using System;
class Shape {
double a_width;
double a_length;
// Default constructor
public Shape()
{
Width = Length = 0.0;
}
// Constructor for Shape
public Shape(double w, double l)
{
Width = w;
Length = l;
}
// Construct an object with
// equal length and width
public Shape(double y)
{
Width = Length = y;
}
// Properties for Length and Width
public double Width
{
get {
return a_width;
}
set {
a_width = value < 0 ? -value : value;
}
}
public double Length
{
get {
return a_length;
}
set {
a_length = value < 0 ? -value : value;
}
}
public void DisplayDim()
{
Console.WriteLine("Width and Length are "
+ Width + " and " + Length);
}
}
// A derived class of Shape
// for the rectangle.
class Rectangle : Shape {
string Style;
// A default constructor.
// This invokes the default
// constructor of Shape.
public Rectangle()
{
Style = "null";
}
// Constructor
public Rectangle(string s, double w, double l)
: base(w, l)
{
Style = s;
}
// Construct an square.
public Rectangle(double y)
: base(y)
{
Style = "square";
}
// Return area of rectangle.
public double Area()
{
return Width * Length;
}
// Display a rectangle's style.
public void DisplayStyle()
{
Console.WriteLine("Rectangle is " + Style);
}
}
// Inheriting Rectangle class
class ColorRectangle : Rectangle {
string rcolor;
// Constructor
public ColorRectangle(string c, string s,
double w, double l)
: base(s, w, l)
{
rcolor = c;
}
// Display the color.
public void DisplayColor()
{
Console.WriteLine("Color is " + rcolor);
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
ColorRectangle r1 = new ColorRectangle("pink",
"Fibonacci rectangle", 2.0, 3.236);
ColorRectangle r2 = new ColorRectangle("black",
"Square", 4.0, 4.0);
Console.WriteLine("Details of r1: ");
r1.DisplayStyle();
r1.DisplayDim();
r1.DisplayColor();
Console.WriteLine("Area is " + r1.Area());
Console.WriteLine();
Console.WriteLine("Details of r2: ");
r2.DisplayStyle();
r2.DisplayDim();
r2.DisplayColor();
Console.WriteLine("Area is " + r2.Area());
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
