constructor

couldn't understand the program

Page 1 of 1

5 Replies - 824 Views - Last Post: 06 December 2008 - 12:11 PM Rate Topic: -----

#1 purewal27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-November 08

constructor

Post icon  Posted 05 December 2008 - 02:16 PM

The program should include two classes Book and Author. A Book should contain a title and publication year. An Author should contain a name and an array of Books. In addition to a constructor that can be used to initialize a Book, the class should define a displayValues() method that outputs the values of the Book object. The Author class should contain a constructor used to initialize an Author, a displayValues() method, and a method addBook(Book). The main() function of the program should create two Author objects and several Book objects assigned to each Author. The displayValues() method of the Author objects should be used to output the values for each Author.

my problem is, i couldn't really understand the program. i understood half of it. and i actually wrote it. but i am confused about the output. i wrote to my professor but didn't get any reply yet. i really need to finish my program today. i'll really appreciate if somebody can help me. do we have to promt the user for data and then display the data with displayValue() method or are we going to set the values in the program and then display them with displayValue mathod. can somebody give me a hint how the output suppose to look like with this program.
i can show what i have wrote if somebody want to look at it. but it is not working . i have some errors.

Is This A Good Question/Topic? 0
  • +

Replies To: constructor

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: constructor

Posted 05 December 2008 - 03:40 PM

So, show us your code and your errors. We're not mind readers, ya know?
Was This Post Helpful? 0
  • +
  • -

#3 purewal27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-November 08

Re: constructor

Posted 05 December 2008 - 03:50 PM

this is the program that i have created.
#include<iostream>
#include<conio.h>
using namespace std;
class book
{
public:
  string title;
  int year;
public:
   void setValues(string,int);
   void displayValue();
};
void book :: setValues(string bkTitle, int bkYear)
{
title = bkTitle;
year = bkYear;
}
void book :: displayValue()
{
cout<<" The title of the book is: "<< title<<endl<<" The publication year is: "<<year<<endl;
}
class author
{
public:
  string name;
  book obj[10];
  int i;
public:
  void displayValues();
  void addBook();
};
void author :: displayValues()
{
cout<<" Name of the Author is :"<<name<<endl;
cout<<" Book Written :"<<obj[i].title<<endl;
cout<<" Published in:"<<obj[i].year<<endl;
}	
void author :: addBook(book obj1)
{
 obj[i] = obj1;
 cout<<"Enter the name of the author";
 cin>>name;
 cout<<"Enter the name of the book";
 cin>>obj[i].title;
 cout<<"Enter the year when the book is published";
 cin>>obj[i].year;
 i++;
 }
 
int main()
{
book book1[10];
book book2[10];
book1.displayValue();
author author1;
author author2;
for(int i=0;i<2;i++)
   {
	author1.addBook(book1[i]);
	 }
for(int j=0;j<2;j++)
   {	 
	author2.addBook(book2[i]); 
   }
return 0;
}


these are the errors:

Error E2316 test1.cpp 39: 'author::addBook(book)' is not a member of 'author'
Error E2294 test1.cpp 54: Structure required on left side of . or .* in function main()
Error E2227 test1.cpp 59: Extra parameter in call to author::addBook() in function main()
Error E2451 test1.cpp 63: Undefined symbol 'i' in function main()
*** 4 errors in Compile ***
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: constructor

Posted 05 December 2008 - 03:58 PM

:code:

You declare addBook as not taking any parameters, but define it as taking one:
void addBook();
vs.
void author :: addBook(book obj1)


You're declaring two arrays of 10 books, but not setting the books within the arrays. You also can't call displayValue on an array, you have to call it on one of the books in the array.

In your second loop you're using i instead of j to access the book array.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: constructor

Posted 05 December 2008 - 04:10 PM

You also have not declared or defined any constructors for your classes, which appears to be part of your assignment.

EDIT: Output looks like this:
 Name of the Author is :Stephen King
 Book Written :It
 Published in:1987
 Book Written :The Stand
 Published in:1985

This post has been edited by JackOfAllTrades: 05 December 2008 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 purewal27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-November 08

Re: constructor

Posted 06 December 2008 - 12:11 PM

thank you very much
i really appreciate it


View PostJackOfAllTrades, on 5 Dec, 2008 - 03:10 PM, said:

You also have not declared or defined any constructors for your classes, which appears to be part of your assignment.

EDIT: Output looks like this:
 Name of the Author is :Stephen King
 Book Written :It
 Published in:1987
 Book Written :The Stand
 Published in:1985

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1