Passing an array to a constructor

Hi,

I have my code here:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


class Author
{
private:
	string aut_name;
	string aut_email;
	char aut_gender;

public:
	Author(string name = "aaa", string email ="eeee@email", char gender='m') : aut_name(name), aut_email(email), aut_gender(gender)
	{
	}

	string getAutName()
	{
		return aut_name;
	}
	string getAutEmail()
	{
		return aut_email;
	}
	void setAutEmail(string email)
	{
		aut_email = email;
	}
	char getAutGender()
	{
		return aut_gender;
	}
};

class Book: public Author
{
private:
	string bookName;
	double price;
	int qty;

public:
	
	Book(string name, double price=0) : bookName(name), price(price)
	{}
	Book(string name, double price=0, int qty = 0) : bookName(name), price(price), qty(qty)
	{}

	string getBookName();
	friend ostream& operator<<(ostream &out, Author &author);
	double getBookPrice();
	void setBookPrice(double price);
	int getBookQty();
	void setBookQty(int qty);
	
};

string Book::getBookName()
{
	return bookName;
}


//ostream& operator<<(ostream &out, Author &author)
//{
	//out << author.getName() << " "<< author.getEmail() << " "<< author.getGender()<<" ";
	//return out;
//}

double Book::getBookPrice()
{
	return price;
}
void Book::setBookPrice(double price)
{
	this->price = price;
}
int Book::getBookQty()
{
	return qty;
}
void Book::setBookQty(int qty)
{
	this->qty = qty;
}



int main()
{

	Author authors[2];
	authors[0] = Author("author 1", "email 1", 'f');
	authors[1] = Author("author 2", "email 2", 'm');

	Book b1("C++ for dummies", authors, 19.99, 99);
	
	return 0;
}


As you see, I would like to pass the authors objects array, to Book constructor, but I don't know how can I make this.
Can you give me some advices please ?
Also I would like some exercises with pointers to classes/objects/functions etc. if you know where I can find...I would like a lot of exercises like on this website (hope is ok if I post this).
https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html

Thank you!
Last edited on
Hey alx119, to answer the question you just think of functions and arrays. That's really all.
Now to the more elaborated explanation, you have (ignore this part if you already know about passing array parameter that is written like this):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class author
{
public:
    author(string name, string email);
private:
    string name, email;
};
author::author(string name, string email)
{
    cout<<name+'\n'+email;
}
class book:public author
{
public:
    book(author newAuthor[2], int newPrice);
private:
    int priceInCents;
};
book::book(author newAuthor[2], int newPrice)
{
    name=newAuthor[0].name+" and "+newAuthor[1].name;
    email="Contact at: "+newAuthor[0].email+" and "+newAuthor[1].email;
    priceInCents=newPrice;
}

Think of the Author class as a variable. It can be an array, and if you want to pass an array as paramater to a function, you write like this:
 
void functionGetArray(int intArray[100]);

Now the object is no different, except that it is user-defined.
If you do
1
2
3
int intArray[2];
//or
author newAuthor[2];

, then it's no different in writing the parameter. If you know from the beginning that it's a two-component array, you just simply write a parameter that is a two-component array.
book(author newAuthor[2], int newPrice);



But maybe you don't know how many components the array has got at a certain point, so you then need a pointer instead of an array. The array is something you just say is this or that when you start the program (and never resizes throughout the program), and the pointer is something you can always add/remove nodes/components to. Therefore (if you know how to handle pointers and/or lists), change
1
2
3
4
//from
book(author newAuthor[2], int newPrice);
//to
book(author *newAuthor, int newPrice);

But keep in mind that it's not just passing it as a pointer, you also have to do the operations specific to pointers to make it possible.
As for exercises, here are some that are even practical:

1. class circle;
Here you should use a main method that defines a circle if there isn't one inside the object. To keep the circle defined inside, just use a string object. Also add a method which draws another circle in the string, a fillCircle() function, and so on (things that are helpful when you create a circle and want to do something to it while it's outside of the object, line in a string in the main() function).

2. a class for buttons:
Just a class that outputs images of buttons (either predefined or even customized by the user, or taking the texture/text from a file) and an action that the button does.
An example of what I once did is writing an options class. With it you could enter "background" and "font" color sections, and change it in real time. The console would literally go through the rainbow with its colors (one at a time).

3. something that is game based:
Use physics, such as falling, jumping, getting pushed, bumping into a wall and so on, creating specific methods.
Eg.:
1
2
3
4
5
6
7
8
void character::functionFall()
{
    //underCharacter is a fragment of string present under your character (can be a square 5x5)
    if (underCharacter=="     ")
    {
        yCoordinate--;
    }
}

(But of course there's more to it that what i've just shown you.)

4. and, the most important one, a bigInt class.
Make 2 of these objects meet in adding, subtracting, multiplying, dividing, verifying equality and attributing, making them practically infinitely large if you want them to be.
Thank you very much for your reply.I understood what you told me.I wanted to make something like this:

 
Book(string name, double price=0, int qty = 0, Author authors[2]) : bookName(name), price(price), qty(qty), aut_name(authors[0].aut_name + " " + authors[1].aut_name), 

I know this is wrong but, how can I explain..I wanted to make the 2 constructors newAuthors, from the Book constructor. :D.Maybe i'm complicating myself.

And another thing...maybe I'm wrong...but it shouldn't give an error your code ?Because "name" should be one for object newAuthor[0] and one for newAuthor[1], but you combined 2 names in one.

And another thing...maybe I'm wrong...but it shouldn't give an error your code ?Because "name" should be one for object newAuthor[0] and one for newAuthor[1], but you combined 2 names in one.

I think you're right. Now i've noticed that instead if I wanted to change the string I would have created a new method inside book:
 
string &book::returnName();

, which could change the string via this. I know it is kinda useless, but anyway.
Maybe it's got a lot of bugs in there, who knows? I just typed something to try and say what I wanted, even if it doesn't work. It's like a prototype, people can do something when they see a draft.

As for
1
2
Book(string name, double price=0, int qty = 0, Author authors[2]) : bookName(name), price(price),
qty(qty), aut_name(authors[0].aut_name + " " + authors[1].aut_name), 

I honestly don't know. I've only seen such a thing once (constructor():function(), function()), and I don't know what it does.

 
I wanted to make the 2 constructors newAuthors, from the Book constructor.

I think that you can't really. Since you create the author class with one constructor, then it's defined that way. If you want to add another constructor inside author later, then it means that some objects might not get another one.

How to solve? Imagine like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class author
{
public:
    function1();
    function2();
};
class book:public author
{
public:
    constructor(); //book();
    variable2;
};
//this means that, by definition, it automatically turns class book into
class book
{
public:
    function1();
    function2();
    constructor(); //book();
    variable2;
};


EDIT

P.S.: If you want to add another constructor, do it 1 of 2 ways:
1. inside author just define another constructor, because if you don't use it then it's not a big loss or anything
2. define author1 with that constructor, and author2:public author1 in which you add the second constructor
Last edited on
Thank you so much Troaat for your replies.Forget about the constructors in another constructors.I am beginner and I'm complicating to much. :P
By the way, I appreciate your exercises that you gave, but...do you have something easier ? :D
I will keep those ones for a little bit later, when I will know better c++. :)

Wait for your reply.Thanks again. :)
One problem is in the design here:
 
class Book: public Author


That's saying that a Book is a type of Author. It doesn't make sense.

The Java page referenced in the OP doesn't say that. It says a Book has an Author. That is, the Author is a member variable within the Book class.

After completing that example it goes on to considering a book which could have multiple authors. Here, you could use an Author array, or since this is C++, a std::vector to store all of the authors.


That's what I was trying to do. But maybe not all the things from java, work on c++ as well. :)
Easier exercises:
1. Add, via a method, the values from two objects (a function for 1 variable, or even both variables at once)
2. Based on a string input, the program will do various calculations with numbers (random or input by user. The string will be checked by a method and will call other methods to do the rest of the job)
3. Create a 25-people database class so that you can easily input new information about people, add new people and view information from the database file
That's what I was trying to do. But maybe not all the things from java, work on c++ as well.
Of course there are differences in the two languages. But the main difference here is that you used a different design.

Instead of inheritance, make the Author a member variable of class Book.

Start like this:
1
2
3
4
5
6
7
class Book
{
private:
	string bookName;
	double price;
	int qty;
	Author author;


and when you have it working for a single author, you might try
1
2
3
4
5
6
7
class Book
{
private:
	string bookName;
	double price;
	int qty;
	vector<Author> authors;
I don't know why I thought that on that site I had to use inheritance, but now I realised that I shouldn't. :D
I will give it a try.
Topic archived. No new replies allowed.