C# Programming Illustrated Guide For Beginners & Intermediates by William Sullivan
Author:William Sullivan
Language: eng
Format: epub
Tags: C sharp programming, object oriented programming, C# & Operators, Learn Coding Fast, Hands on Project, Constants, Data types, Operators & Compiler, jump statement, Arrays & Lists, Loops & Variables & Literals, C# strings, polymorphism, flow statements, Absolute Beginners Guide, Practical Examples
Publisher: Healthy Pragmatic Solutions Inc
Published: 2019-08-17T00:00:00+00:00
Constructor Overloading
You can have more than one constructor per class. The names of all the constructors will always be exactly the same as the name of the class; the difference lies in the numbers and types of parameters. Having more than one constructor in a class refers to constructor overloading. Letâs see this in action:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
// Creating Object of the Airplane Class
AirPlane plane1 = new AirPlane("Airbus", "A-380");
AirPlane plane2 = new AirPlane();
// Accessing class members from plane1 object
Console.WriteLine(plane1.planeMake);
Console.WriteLine(plane1.planeModel);
// Accessing class members from plane2 object
Console.WriteLine(plane2.planeMake);
Console.WriteLine(plane2.planeModel);
Console.ReadKey();
}
}
class AirPlane
{
public string planeMake;
public string planeModel;
public AirPlane(string make, string model)
{
planeMake = make;
planeModel = model;
}
public AirPlane()
{
planeMake = "Lockhead Martin";
planeModel = "F-16";
}
public void StartPlane()
{
Console.WriteLine("Plane Started");
}
public void StopPlane()
{
Console.WriteLine("Plane Stopped");
}
}
}
In the script above, we have two constructors in our âAirPlaneâ class, one of the constructors is parameterized while the other constructor doesnât accept any parameters.
In the âMainâ method, we create two objects of the âAirPlaneâ class: plane1 and plane2. The former is created using the parameterized constructor while the latter is created using the simple non-parameterized constructor. The output of the script above looks like this:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9915)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9778)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8297)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6842)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6570)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6436)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4317)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(4002)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3794)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3737)
The Ultimate iOS Interview Playbook by Avi Tsadok(3712)
