No problems here, just need explanation how does that work.
I was doing homework for my C# class and I managed to do it by myself by following code examples provided by our professor. The problem is I don't get how it works. Here are things that boggle me:
First of all, how come I need to use xmlBook.Title = "XML Primer Plus";
instead of Book clrBook = new Book("CLR via C#", ...")
and vice-versa as constructors.
Second, why I don't have to have any parameters when using : base()
?
Third, how does overwrite by using new public void display()
only adds output, instead of completely modifying original protected void display()
? I guess because the original diplay()
is protected?
Please clarify
Regards.
Main.cs
using System;
namespace Lab_4
{
class Program
{
static void Main(string[] args)
{
Book xmlBook = new Book();
xmlBook.Title = "XML Primer Plus";
xmlBook.AuthorFirstName = "Nicolas";
xmlBook.AuthorLastName = "Chase";
xmlBook.Price = 44.99F;
xmlBook.PublisherName = "Sams Publishing";
Book clrBook = new Book("CLR via C#",
"Jeffrey",
"Richter",
59.99f,
"Microsoft Press");
Console.WriteLine("=== xmlBook ===");
xmlBook.display();
Console.WriteLine();
Console.WriteLine("=== clrBook ===");
clrBook.display();
}
}
}
Publication.cs
using System;
namespace Lab_4
{
public class Publication
{
string publisherName, title;
float price;
public Publication()
{
}
public Publication(string title,
string publisherName,
float price)
{
Title = title;
PublisherName = publisherName;
Price = price;
}
public float Price
{
set
{
price = value;
}
}
public string PublisherName
{
set
{
publisherName = value;
}
}
public string Title
{
set
{
title = value;
}
}
protected void display()
{
Console.Write("{0}\n{1}\n{2}\n", title, publisherName, price);
}
}
}
Book.cs
using System;
namespace Lab_4
{
public class Book : Publication
{
string authorFirstName, authorLastName;
public Book()
{
}
public Book(string bookTitle,
string firstName,
string lastName,
float bookPrice,
string publisherName)
: base()
{
Title = bookTitle;
AuthorFirstName = firstName;
AuthorLastName = lastName;
Price = bookPrice;
PublisherName = publisherName;
}
public string AuthorFirstName
{
get
{
return authorFirstName;
}
set
{
authorFirstName = value;
}
}
public string AuthorLastName
{
get
{
return authorLastName;
}
set
{
authorLastName = value;
}
}
new public void display()
{
base.display();
Console.WriteLine("{0}", getAuthorName());
}
string getAuthorName()
{
return AuthorFirstName + " " + AuthorLastName;
}
}
}