467,923 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

C++ Book program with classes

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
  • viewed: 2415
Share:
1 Reply
weaknessforcats
Expert Mod 8TB
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.

Similar topics

9 posts views Thread by Dragon | last post: by
13 posts views Thread by YoVoltron | last post: by
14 posts views Thread by At_sea_with_C | last post: by
4 posts views Thread by Johs | last post: by
11 posts views Thread by allendowney | last post: by
6 posts views Thread by nembo kid | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.