By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

C++ Book program with classes

P: 5
i got this program the other day and ive just started it and i am getting some errors that i cant figure out.

requirements: 1)create a clas called Book. a Book has three data members: m_title, m_id and m_flag to tell whether the book is in or checked out. 2)Write a constructor for Book. write a constructor that takes 3 parameters: Book(char * title, int id, bool flag) and initializes the 3 data members accordingly. in addition print out a message inside your constructor whenever it gets called. 3) Instantiate or create a few Book objects to test your constructor in main()-notice when your constructor is called. Book book1("Petzlod", 100, false);
Book book2("Nagler", 101, true);
Book book3("Meyers", 102, false);
this is where im stuck at. heres the code:

// file: book.h
// CST 136
// Class definition for book class


#ifndef BOOK_H
#define BOOK_H


class Book{

private:
char m_title;
int m_id;
bool m_flag;

public:

Book();
Book(char * title, int id, bool flag); // 3 argument constructor


}

#endif

************************************************** ************************************
// file: book.cpp
// CST 136
// code for the member functions of class Book

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "book.h"


Book::Book()
{

}

Book::Book(char * title, int id, bool flag)
{
char * m_title = title;
int m_id = id;
bool m_flag = flag;
}

************************************************** ************************************
main.cpp

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "book.h"

void main()
{
Book book1("Petzlod", 100, false);
Book book2("Nagler", 101, true);
Book book3("Meyers", 102, false);


}

************************************************** ************************
heres the errors: 1)'Book::{ctor}' : constructors not allowed a return type
2)'Book' followed by 'void' is illegal (did you forget a ';'?)
3)return type of 'main' should be 'int' instead of 'Book'

any help will work and be appreciated. thanks
Jan 27 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
1) You forgot the semi-colon at the end of your class declaration.

2) Your data member for the title is one char. That's a short title.
I recommend a string object. If that's not possible, then use an array.
If you use an array, you will need to copy the constructo argument to the array.

3) main() returns an int. Only as Microsoft does main() return a void- and they have been backing away from that screw-up for years.
Jan 27 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.