i need help i wanna know how can i pass am array of objects to the constructor header & the .cpp implementation ? & prompt the user to enter the values to fill it ?
& how to pass it if it's array of int & its multi dimension ?

thanks

Recommended Answers

All 13 Replies

Please post your attempt at this problem.

OK ..... i have to do my homework .... &i have problem of passing arrays to the functions & problem with function ( Add-shop ) ... how can i add an object to array shops ...

this my homework !!

Create a class called Mall that represent any mall in Jordan. Write a driver program to test your class
(make sure to test all the member functions inside your class). The private data members of this class
include the following:
− A string variable which contains the mall name.
− An array of objects from class Shop which contains all shops in this mall. The size of this array
is 10.
− A two dimensional array of integers of size 10x2 where each row of this array contains the floor
number in which the shop is located and the second row is the shop area in m2. Inside the
constructor prompt the user to enter the values to be filled in this array.
− Const integer variable which is the size of the mall parking, i.e. max number of cars it can
withstand.
Provide a constructor function that enables an object of this class to be initialized when it is declared.


Class Shop which represents small and mid size shops. Your class contains the following private
data members:
• A string variable which is the name of the shop.
• Array of objects from class Item with size equals 10.
Provide a default constructor function that enables an object of this class to be initialized when it
is declared. Also, provide public member functions for each of the following:
a) set and get functions for all private data members.
b) Function called Display which displays all the information, i.e. data members values
related to a specific Shop object.

Class Item which represents any item sold in a shop. Your class contains the following private
data members:
• A string variable which is the name of the item.
• Integer variable which is the price of the item.
• Integer variable which is the total number of entities available from this item inside the
shop.
Provide a default constructor function that enables an object of this class to be initialized when it
is declared. Also, provide public member functions for each of the following:
a) set and get functions for all private data members.
b) Function called Display which displays all the information, i.e. data members values
related to a specific Item object.
Returning to class Mall, for this class you should provide the following public member functions:
a) AddShop: add a student to the array of students to a specified location in this array (i.e. takes the
student object and the array index as parameters).
b) Largest: returns the shop with the largest area in the mall.
c) Cheapest: returns the shop with the lowest average items price within the mall.
d) Expensive: returns the item with the least price in a specific shop within the mall.
e) TotalPrices: returns the sum of prices of all items in all shops within the mall.
f) Display: which displays all the information, i.e. data members values related to a specific Mall
object.

thank you so much ^^

i have to do my homework ....

Yes, that is the key here. You've posted your assignment, but what have you done with it so far? Try to code as much of it as you can, and post back with specific problems. There's no need to put everything in bold my eyesight is still good.

ok i am sorry ... i tried to solve i asked the engineer who teaches me ...i didn't get what i want ...

i know my code is not that good but i am in my beginnings in programming ...here my code ... i have some errors i am tracking them ...i have problem with function add shop & function expensive i am working on them ...

mall.h ....

#ifndef MALL_H
#define MALL_H
#include "shop.h"

class mall
{
	public :
	mall ( string , const int );
	void setshoplace ();
	void setshops ( );
	void display ();
	void AddShop(shop , int p);
	shop largest ();
	shop cheapest ();	item Expensive ();
	int totalprice ();

    private :
	string mallName ;
	shop allshops [10];
	int shoplace [2][10];
	const int parkingsize;
	};
#endif

shop.h

#ifndef SHOP_H
#define SHOP_H
#include <string>
using namespace std ;
#include "item.h"
class shop {
    public :
	shop (string);
	void setshopitem (item [10]);
	int getshopitem ();
	void setshopName (string) ;
	string getshopName ();
	void Display ();

    private :
	string shopname ;
	item shopitem [10];

};
#endif

item.h

#ifndef ITEM_H
#define ITEM_h

#include <string>
using namespace std ;

class item {
public :
	item (string ="item" , int=50 ,int=100 );
	void setitenName (string );
	string getitemName ();
	void setitemprice (int);
	int getitemprice ();
	void setotitem  (int);
	int getotitem ();
	void Display ();
	
private :
	string itemName ;
	int itemprice ;
	int totitem ;


};
#endif

.cpp files

i don't wanna use dynamic memory because we haven't taken them yet !

mall.cpp
its not complete ...

#include <iostream>
#include <string>
using namespace std;

#include "mall.h"

mall::mall ( string mn  , const int s=100 ):parkingsize (s)
{
	cin >> mn ;
	mallName = mn ;
	  setshoplace () ;
	  setshops () ;
}void mall::setshoplace (){
	int s [2][10];
	for (int i = 0; i<2; i++){
    for ( int j = 0 ; j<10; j++)
    cin >> s[i][j] 
	 
	shoplace [i][j]= s[i][j]}
}
void mall :: setshops (){
	shop L[10] ; 
	for (int  i=0 ; i<10 ; i++){
		cin >> shop L[i] 
		allshops [i]= shop L[i]}
}

void  AddShop(shop a , int p){

shop.cpp

#include <iostream>
#include "shop.h"
shop::shop (string SN = "shop" ) {
	shopname = SN ;
	item SI [10];
	setshopitem (SI[10]) ;
}

void shop :: setshopitem (item SI[10] ){
	for ( int i =0 ; i<10 ; i++ )
    shopitem [i] = item SI[i];
}
int shop :: getshopitem (){
	
	for (int i = 0 ; i<10 ; i++){
		return shopitem [i]
	}
}

void shop :: setshopName (string name ){
	shopname = name;
}
string shop :: getshopName (){
	return shopname ;
}
void Display (){
	cout << "shop name is " << shopname << endl;
	for (int i =0 ; i<10 ; i++)
		cout << " shop item is " << shopitem [i] <<endl;
}

i have errors here too i am tring to figure them out
item.cpp

#include <iostream>
using namespace std;
#include "item.h"
item :: item (string Itn , int itemprice , int totitem ){
	itemName = Itn;
     itemprice= itemprice ;
	 totitem = totitem;
}
void item :: setitenName (string Itn ){
	itemName = Itn; 
}
string item :: getitemName (){
	return itemName ;
}
void item :: setitemprice (int itp){
	itemprice=itp ;
}
int item :: getitemprice (){
	return itemprice;
}
void item :: setotitem  (int tot){
	totitem =tot ;
}
int item ::  getotitem (){
	return totitem ;
}
void item :: Display (){
	cout << itemName <<itemprice << totitem <<endl;
}

thank you

Okay, we're getting closer, but you need to point out where the errors are. It is not my responsibility to compile your program and interpret the errors.

i didn't get what i want ...

He/she probably didn't want to do it for you, either.

sorry i am new here ... i didn't have the time to see the forum's policy .. !! so when i posted my problem in another thread i didn't mean to disorganize the the place ...

mall.cpp

errors

[IMG]http://i55.tinypic.com/34i47ib.png[/IMG]

For the first error, you don't have to specify the datatype when you are reading in. What you need to do for the first one, is overload the >> operator of your shop class. As it stands, you're trying to input the name of the shop, but the class has to know which variable you want to store it in, it cannot guess.

You can either modify your class to have an overloaded >> operator (see http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/ostreamandistreamoperatorforaclass.htm, ignore their egregious <iostream.h> at the top and all the other info is good), or you can read in the shop names as strings via cin and use your setshopName to take those strings and write them into your objects.

shop.cpp

[IMG]http://i54.tinypic.com/11c59oi.png[/IMG]

just those 2 have errors

The first one, what you are trying to pass in is the element that's one past the end of the array (which doesn't exist).

int arr[30]; //ok, need to specify the length when declaring

int temp = arr[30]; //no, indexes run from 0 to 29 in this array
int temp2 = arr[29]; //okay, we're assigning one integer to another

Just pass in SI, not SI[10] for the top error

For the second error on that page, you don't need the datatype again, just like you didn't on the other page.

Re-read your getshopitem method too, there's a typo that's going to stop your for loop from doing anything, but even after you fix it, it's only going to return one value. See if you can see why.

I'm not sure what it's complaining about on the last 2 errors, but see if some of the prior errors straighten those out. If not, mouse over them to see what the compiler is complaining about. The specific error messages are important.

i didn't ask her to do it for me !!

I apologize, that was meant to be more joking that serious or angry.

I will say this, though, in all seriousness, when approaching someone for help, you need to meet them more than halfway. I shouldn't have had to get the errors out of you piecemeal, you should have presented the errors (with the text of those errors, with any necessary code) straight away. Just something to bear in mind.

thank you so much

but

int shop :: getshopitem (){
 
for (int i = 0 ; i<10 ; i++){
return shopitem [i]
}
}

semicolon maybe after

return shopitem [i] ;

!!
but why would it return one value it's inside the for loop , should i return it with function call ?
it will return an object from function item with specific index , we still didn't take operator overloading , but i will look for more information about it from Google ...
^___^

thank you so much

but

int shop :: getshopitem (){
 
for (int i = 0 ; i<10 ; i++){
return shopitem [i]
}
}

semicolon maybe after

return shopitem [i] ;

!!
but why would it return one value it's inside the for loop , should i return it with function call ?
it will return an object from function item with specific index , we still didn't take operator overloading , but i will look for more information about it from Google ...
^___^

Yes, you definitely need one after return shopitem[i] . In the image that you posted, you had an extra semicolon after the for loop. Remember that without braces, the for loop will only execute the next statement. Think of it being like:

for (int i = 0;i<10;i++);
   std::cout<<"Does this get executed 10 times?"<<std::endl;

is like saying

for (int i = 0;i<10;i++)
  ;
std::cout<<"Does this get executed 10 times?"<<std::endl;

//it only gets executed once either time

For that particular method, you're probably better off doing something like:

int shop :: getshopitem (int i){
 
  return shopitem [i]; //you could check that the index is valid, too.
}

because the first time through that loop, the code would return and then that's the end of the method call.

If you haven't done operator overloading, just read in strings and use your "setter" method to set the store name.

thank you so much for helping me ...

in this forum we use English language i mean we cant use another language , right ?

You're welcome.

i mean we cant use another language , right

Forum posts must be in English, yes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.