Creating two objects - book1 and book2

how can one write a program containing 2 objects - book1 and book2.
I need an explanation on how to do that
closed account (z05DSL3A)
http://www.cplusplus.com/doc/tutorial/
You can create a class for it.

1
2
3
4
5
6
7
8
9
10
//This is the class declaration
class book_one
{
     //Add in characteristics
     int pagenumber=400;
     //You can add functions too
     void openbook();
     //This is where you would define the function
}book_one;     //After a class you should probably name it as an object.  Here I named this "book_one."
               //You need the semicolon at the end. 


You would do this for both of the books, as well as any class you would make. Then you would incorporate this into the rest of your program.

Hopefully that helped!


Gobblewobble123
Last edited on
class library
{
int br_code;
int title_code;
} book1, book2;


is above peace of code correct for an object 'library' and how can i incorporate a program.

thanks for the head way.
Well, I wouldn't call it a library. That is correct syntax, and this creates two objects, named book1 and book2. One thing I forgot though, is the word "public:." What this does is, it allows any other part of the program to access and modify the code under "public (you need a colon after public.)" I coded up a quick program for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

class book
{
public:
	int pages;
	void readbook()
	{
		cout<<"Reading!\n";
	}
}book1,book2;

int main()
{

	book1.readbook();
	book2.readbook();	
	
return 0;
}
Reading!
Reading!



This program just calls the function "readbook." It's easy to call; you just do the class name then a period, then the function name.
Last edited on
Topic archived. No new replies allowed.