/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example7_5.cs illustrates versioning
*/
using System;
// declare the MotorVehicle class
class MotorVehicle
{
// declare the fields
public string make;
public string model;
// define a constructor
public MotorVehicle(string make, string model)
{
this.make = make;
this.model = model;
}
// define the Accelerate() method
public virtual void Accelerate()
{
Console.WriteLine("In MotorVehicle Accelerate() method");
Console.WriteLine(model + " accelerating");
}
}
// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{
// define a constructor
public Car(string make, string model) :
base(make, model)
{
// do nothing
}
// define the Accelerate() method (uses the new keyword to
// tell the compiler a new method is to be defined)
public new void Accelerate()
{
Console.WriteLine("In Car Accelerate() method");
Console.WriteLine(model + " accelerating");
}
}
public class Example7_5
{
public static void Main()
{
// create a Car object
Console.WriteLine("Creating a Car object");
Car myCar = new Car("Toyota", "MR2");
// call the Car object's Accelerate() method
Console.WriteLine("Calling myCar.Accelerate()");
myCar.Accelerate();
}
}
Month: February 2011
Private field and public Property in inheritance
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
namespace nsInherit
{
using System;
public class clsMainInherit
{
static public void Main ()
{
clsDerived derived = new clsDerived();
derived.Property = 42;
derived.ShowField();
}
}
//
// Define a base class with a private field and a public Property
class clsBase
{
private int m_Field;
public int Property
{
get {return (m_Field);}
set {m_Field = value;}
}
public void ShowField ()
{
Console.WriteLine ("The value of m_Field is " + m_Field);
}
}
//
// Define a derived class that inherits from the clsBase
class clsDerived : clsBase
{
// For now, the derived class needs no members
}
}
illustrates inheritance
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example7_1.cs illustrates inheritance
*/
using System;
// declare the MotorVehicle class (the base class)
class MotorVehicle
{
// declare the fields
public string make;
public string model;
// define a constructor
public MotorVehicle(string make, string model)
{
this.make = make;
this.model = model;
}
// define a method
public void Start()
{
Console.WriteLine(model + " started");
}
}
// declare the Car class (derived from the MotorVehicle base class)
class Car : MotorVehicle
{
// declare an additional field
public bool convertible;
// define a constructor
public Car(string make, string model, bool convertible) :
base(make, model) // calls the base class constructor
{
this.convertible = convertible;
}
}
// declare the Motorcycle class (derived from the MotorVehicle base class)
class Motorcycle : MotorVehicle
{
// declare an additional field
public bool sidecar;
// define a constructor
public Motorcycle(string make, string model, bool sidecar) :
base(make, model) // calls the base class constructor
{
this.sidecar = sidecar;
}
// define an additional method
public void PullWheelie()
{
Console.WriteLine(model + " pulling a wheelie!");
}
}
public class Example7_1
{
public static void Main()
{
// declare a Car object, display the object's fields, and call the
// Start() method
Car myCar = new Car("Toyota", "MR2", true);
Console.WriteLine("myCar.make = " + myCar.make);
Console.WriteLine("myCar.model = " + myCar.model);
Console.WriteLine("myCar.convertible = " + myCar.convertible);
myCar.Start();
// declare a Motorcycle object, display the object's fields, and call the
// Start() method
Motorcycle myMotorcycle = new Motorcycle("Harley-Davidson", "V-Rod", false);
Console.WriteLine("myMotorcycle.make = " + myMotorcycle.make);
Console.WriteLine("myMotorcycle.model = " + myMotorcycle.model);
Console.WriteLine("myMotorcycle.sidecar = " + myMotorcycle.sidecar);
myMotorcycle.Start();
myMotorcycle.PullWheelie();
}
}
Demonstrate when constructors are called
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate when constructors are called.
using System;
// Create a base class.
class A {
public A() {
Console.WriteLine("Constructing A.");
}
}
// Create a class derived from A.
class B : A {
public B() {
Console.WriteLine("Constructing B.");
}
}
// Create a class derived from B.
class C : B {
public C() {
Console.WriteLine("Constructing C.");
}
}
public class OrderOfConstruction {
public static void Main() {
C c = new C();
}
}
A multilevel hierarchy 1
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A multilevel hierarchy.
using System;
class TwoDShape {
double pri_width; // private
double pri_height; // private
// Default constructor.
public TwoDShape() {
width = height = 0.0;
}
// Constructor for TwoDShape.
public TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
public TwoDShape(double x) {
width = height = x;
}
// Properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
/* A default constructor. This invokes the default
constructor of TwoDShape. */
public Triangle() {
style = "null";
}
// Constructor
public Triangle(string s, double w, double h) : base(w, h) {
style = s;
}
// Construct an isosceles triangle.
public Triangle(double x) : base(x) {
style = "isosceles";
}
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
// Extend Triangle.
class ColorTriangle : Triangle {
string color;
public ColorTriangle(string c, string s,
double w, double h) : base(s, w, h) {
color = c;
}
// Display the color.
public void showColor() {
Console.WriteLine("Color is " + color);
}
}
public class Shapes6 {
public static void Main() {
ColorTriangle t1 =
new ColorTriangle("Blue", "right", 8.0, 12.0);
ColorTriangle t2 =
new ColorTriangle("Red", "isosceles", 2.0, 2.0);
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
t1.showColor();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
t2.showColor();
Console.WriteLine("Area is " + t2.area());
}
}
Call a hidden method
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Call a hidden method.
using System;
class A {
public int i = 0;
// show() in A
public void show() {
Console.WriteLine("i in base class: " + i);
}
}
// Create a derived class.
class B : A {
new int i; // this i hides the i in A
public B(int a, int b) {
base.i = a; // this uncovers the i in A
i = b; // i in B
}
// This hides show() in A. Notice the use of new.
new public void show() {
base.show(); // this calls show() in A
// this displays the i in B
Console.WriteLine("i in derived class: " + i);
}
}
public class UncoverName123 {
public static void Main() {
B ob = new B(1, 2);
ob.show();
}
}
Using base to overcome name hiding
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using base to overcome name hiding.
using System;
class A {
public int i = 0;
}
// Create a derived class.
class B : A {
new int i; // this i hides the i in A
public B(int a, int b) {
base.i = a; // this uncovers the i in A
i = b; // i in B
}
public void show() {
// this displays the i in A.
Console.WriteLine("i in base class: " + base.i);
// this displays the i in B
Console.WriteLine("i in derived class: " + i);
}
}
public class UncoverName1231 {
public static void Main() {
B ob = new B(1, 2);
ob.show();
}
}

