tags:

views:

169

answers:

8

Forgive me for asking what some might think are stupid questions. I am trying to read the code below and make sense out of it.

Why is novel.title used in the main. I understand where I get the title from but why does novel.title used. The other one is what is holding the title information that was entered by the user. Same questions with author, and level.

 using System;
 using System.Collections.Generic;
 using System.Text;

 namespace assignment6
{
public class Book
{
    protected string title;
    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            title = value;
        }
    }
    protected string author;
    public string Author
    {
        get
        {
            return author;
        }
        set
        {
            author = value;
        }
    }
    public void showBook()
    {
        Console.WriteLine("The Book you entered is " + title + " by " + author);
    }
    public Book(string booktyp1, string booktyp2)
    {
        Console.WriteLine("You will enter data for a " + booktyp1 + " book indicating the " + booktyp2 + " book.");
        Console.WriteLine();
    }
}

public class Fiction : Book
{
    private int level;
    public int Level
    {
        get
        {
            return level;
        }
        set
        {
            level = value;
        }
    }
    public void showFiction()
    {
        showBook();
        Console.WriteLine("Reading level is " + level);
    }
    public Fiction()
        : base("Fiction", "reading level")
    {
    }
}

public class NonFiction : Book
{
    private int pages;
    public int Pages
    {
        get
        {
            return pages;
        }
        set
        {
            pages = value;
        }
    }
    public void showNonFiction()
    {
        showBook();
        Console.WriteLine(pages + " pages");
    }
    public NonFiction()
        : base("NonFiction", "Number of pages")
    {
    }
}

public class assignment6
{
    public static void Main(string[] args)
    {
        char choice;

        Console.Write("Do you want Fiction (F) or Non-Fiction (N)? -->");
        choice = Convert.ToChar(Console.ReadLine().ToUpper());
        switch (choice)
        {
            case 'F':
                {
                    Fiction novel = new Fiction();
                    Console.Write("Enter the book title-->");
                    novel.Title = Console.ReadLine();
                    Console.Write("Enter the book author-->");
                    novel.Author = Console.ReadLine();
                    Console.Write("Enter the reading level-->");
                    novel.Level = Convert.ToInt32(Console.ReadLine());
                    novel.showFiction();
                    break;
                }
            case 'N':
                {
                    NonFiction manual = new NonFiction();
                    Console.Write("Enter the book title-->");
                    manual.Title = Console.ReadLine();
                    Console.Write("Enter the book author-->");
                    manual.Author = Console.ReadLine();
                    Console.Write("Enter the number of pages-->");
                    manual.Pages = Convert.ToInt32(Console.ReadLine());
                    manual.showNonFiction();
                    break;
                }
            default:
                {
                    Console.WriteLine("You made an inappropriate entry -- closing program");
                    break;
                }
        }
    }

}

}

I am sure everyone here know how new I am to programming. I can say this I love it when it works, I hate it when it doesnt and makes me want to quiet.

A: 

Title, Author, and Level are attributes of the novel object created at the beginning of the block. The attributes of an object cannot be referenced without a reference to the object itself.

Ignacio Vazquez-Abrams
so when the user enters the title its just stored in title, author,level.
randywhite30
No, they're stored in `novel.title`, `novel.author`, and `novel.level`.
Ignacio Vazquez-Abrams
Technically they are properties (in the C# sense), not attributes. Attributes are a whole different beast.
Jeff M
@randy: be careful with your casing. `title` and `Title` refer to two different things in your program. Your `Main` method is assigning to the `Title` *property* of the `novel` object, which in turn assigns to the `title` *field* of the same object.
Michael Petrotta
@Jeff, "attributes" in the OOP sense, not the .NET sense. Why Microsoft chose to redefine standard computer science terminology is beyond me... probably so people will pay for .NET training. Everything makes sense at last. 1) Steal underwear. 2) Redefine words 3) Profit!
Ben Voigt
+1  A: 

Title is a property. It actually manages field called title. The value of title is used in the Show... metod.

Preet Sangha
but when the user enters the title where is it stored
randywhite30
A: 

novel is an object, you access properties of objects such as Title, Author, and level.

Put it into persepect:

You have a novel and you are asking the user what is the Title of the novel (novel.Title). Then you ask the user: who is the Author of the novel (novel.Author). Finally, you ask for the Level (novel.Level), (which I'm guessing is like novel difficulty to read) of the novel.

thyrgle
+1  A: 

novel is a Fiction object. Since Fiction inherits from Book, Fiction has a Title and Author property. So what

novel.Title = Console.ReadLine();

is doing is reading input from the console and storing it in the title attribute of your novel object through the Title property.

You should take a look at this Properties Tutorial.

vash47
A: 

The novel.Title is specifically referring to the Title in the novel object (the one created by Fiction novel = new Fiction();. Say that more Fiction objects were created:

Fiction novel1 = new Fiction();
Fiction novel2 = new Fiction();
Fiction novel3 = new Fiction();

You'd have three Title fields, but how would you indicate which one you're referring to? That's what the prefix is for. In the above example, for instance, you could refer to the Title innovel3 by using novel3.Title.

Mark LeMoine
A: 

Your question is a little unclear, but I think you're asking about the difference between novel.title and novel.Title and why one is used over the other.

novel.title is a field that can store some data (in this case, a string of text). novel.Title is a property that provides access to that field. The property itself cannot store any data, only the field can. Fields are considered to be part of the implementation details of the class and it's considered to use a bad idea to directly expose that to the user code. The property is to give the programmer the freedom to later change the implementation without breaking compatibility - it could be as simple as renaming the field or it might be changed to do something else entirely.

Evgeny
A: 

The

 protected string title;

cant be accessed directly, but can access through :

public string Title
{
    get
    {
        return title;
    }
    set
    {
        title = value;
    }
}

Notice title different Title here, You can novel.Title but no novel.title

pinichi
A: 

Book is the base/super class where derived class are Fiction and NonFiction

Properties of Book are inherited by the two derived class

Two Derived Class declare their own properties.

Marco