Class objects like array?

I would like to ask few questions about classes, for example we have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Book {
public:
int pages;
};

int main() {

Book one, two, three;

one.pages=10;
two.pages=20;
three.pages=30;

return 0;
}


How can I get the average of all the pages, and how can i count how many objects are in class?
Last edited on
What?
Do you want the average number of page for one, two and three?

PS: it's int main
Yes but not like this (one.pages (plus) two.pages (plus) three.pages)/3, What if there is unknown amount of books, and we need to find average of pages.

(somehow (plus) symbol not shows)
Last edited on
You have to know how many objects you have if you keep separate variables.
If you don't, you need to keep them in some kind of container ( eg: vector, list, array... )
Ok lets say I know how many variables I have, is there any way?
If you have hardcoded variables, the only way is (one.pages two.pages three.pages)/3
unless you fill a container with those and use a loop.
Last edited on
Im not sure what is this container you are talking about, and how to loop it? (with while()? )
Hello,

Please refer to the below code to know how many objects are there in class. In your case you are saying you may not know the number of books (objects in class). You can do so by using static data member (in code below it is index) and set it to zero.
1
2
3
private:
	static int index;

As such this data member will be shared by all the objects. In constructor simply increment this data member. So whenever any object will be initialized this index will increament and will give the number of objects.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include "stdafx.h"
#include "iostream.h"

class sample
{
private:
	static int index; //static data member declaration. This means that index will be shared between all the object
	int count;
public:
	sample()
	{
		
	 index  ;
	}
	~sample()
	{
	}
	
	void showdata() const;
/*	{
		count  ;
		cout<<endl<<"index="<<index<<endl;
	}*/

};

void sample::showdata() const  //this is how defination of member function is done outside the class
{
	
	cout<<endl<<"index="<<index<<endl<<"count="<<count;
}

int sample::index=0;  //defination of static data member is done outside the class
//name of class must be included in defination. Can be used to keep track of how many objects are created for class 

int main(int argc, char* argv[])
{
	sample s1,s2,s3;
	s1.showdata();
	s2.showdata();
	s3.showdata();
	
	return 0;
}

Last edited on
Well, a container could be (as Bazzy listed above) a vector, a deque, a list, an array... the details on how to use all of them are listed below (in order of most to least recommended). :)

http://cplusplus.com/reference/stl/vector/ - Vectors
http://cplusplus.com/reference/stl/deque/ - Deques
http://cplusplus.com/doc/tutorial/dynamic/ - Dynamic Arrays
http://cplusplus.com/reference/stl/list/ - List

Happy coding!

-Albatross
Im not sure what is this container you are talking about, and how to loop it? (with while()? )
A container is a container class ( http://www.cplusplus.com/reference/stl/ ) or an array.
The best way to loop in a sequence container is using a for
I dont know there is some problem in posting the above code.

In constructor, it is index plus plus .

I tried editing it, bit it is not showing plus signs .

Please see to it.
Last edited on
Topic archived. No new replies allowed.