Getting Values from a baseclass into the method of a derived class

Oct 5, 2013 at 8:29am
Please help thank you.

Work is the baseclass
1
2
3
4
5
6
7
8
9
10
Work::Work(void)
{
}

void Work::setWork(int num, double hour, double rates)
{
	worknum = num;
	workhour = hour;
	rate = rates;
}


WorkBid = derived class
1
2
3
4
5
6
7
8
9
10
WorkBid::WorkBid(void)
{
}

void WorkBid::setWorkBid(int bidid, Work bid, double quote)
{
	bidnum = bidid;
	bidwork = bid;
	quotation = quote;
}
Last edited on Oct 6, 2013 at 2:58pm
Oct 5, 2013 at 9:14am
closed account (o3hC5Di1)
Hi there,

I'm afraid we will need a little bit more information than what you are giving in the subject line. Please share your code and specifically tell us what you are trying to do.

All the best,
NwN
Oct 5, 2013 at 9:15am
Please elaborate.
Do you want to create array inside of class which size will be known at compile time?
And how cin relates to your question?
Oct 5, 2013 at 9:34am
Hi,

I have no idea how to start.
I was tasked to set an array to a class.
The number of arrays however is user defined which means that it is not a constant and I cant use the normal method to array the class as it is not a constant.

Please guide me. Thank you so much
Oct 5, 2013 at 10:08am
You should use dynamic allocated array initializated in constructor:
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
class Test
{
  public:
    int* array;
    int size;    

    Test() : size(1)//Default constructor, creating one element array
    {
         array = new int[size];
    }
    
    Test(int n) : size(n) //parametrized constructor, creating array of size n
    {
        array = new int[size];
    }

    ~Test() //destructor: deleting array to prevent memory leak
    {
        delete[] array;
    }
};

//usage:
Test var(6);
for(int i = 0; i < var.size; ++i)
    std::cin >> var.array[i]; //Fill internal array from cin 
Oct 5, 2013 at 10:52am
Hello MiiNiPaa,

I dun quite understand.

I changed my code from your example.
This is a portion of my code in the test driver.

I wanted to assign an array into a class. (RollBid)
but it still state numbits as a non-constant, so I cant assign.

1
2
3
4
	cout << "Enter number of Bids: ";
		cin >> numbids;
		int* array = new int[numbits];
		RollBid bid[numbits];
Oct 5, 2013 at 11:05am
closed account (o3hC5Di1)
Hi there,

Seems you have a typo in here:

1
2
3
cin >> numbids;  //this says numbiDs
int* array = new int[numbits];  //these say numbiTs
RollBid bid[numbits];


I thinkyou will have to share more of your code if you want us to help you more specifically.

All the best,
NwN
Oct 5, 2013 at 11:13am
I'm not really sure what you want

In My Opinion ... I think the proper question is how to create an array of objects.

If you want to create an array of objects, you should do it as follows:

1
2
3
4
5
6
7
8
9
cout << "Enter number of Bids: ";
cin >> numbids;

RollBid* pointer_name = new RollBid [numbids];


/* and somewhere in your main: */
delete [] pointer_name;


note: It would be better to us if you show your class declaration and constructor definition
Last edited on Oct 5, 2013 at 11:21am
Oct 5, 2013 at 11:18am
1
2
3
4
	cout << "Enter number of Bids: ";
		cin >> numbids;
		int* array = new int[numbits];
		RollBid bid[numbits];

Why are you creating an array of ints if you need an array of RollBids?

change third line to RollBid* bid = new RollBid[numbids]; and delete fourth line
Oct 6, 2013 at 2:46pm
Thank you! It worked. Now I have another problem.

I have to get data from a class into another class. But I cant do it correctly.
Please see below for my code. line 33 in int main().
My operator is also not working. Please help thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <iostream>
using namespace std;

class Work
{
private:
	int worknum;
	double workhour;
	double rate;

public:
	Work();
	void setWork (int, double, double);
	double getbaseprice();
	void display();
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include "work.h"

class WorkBid :
	public Work
{
private:
	int bidnum;
	Work bidwork;
	double quotation;

public:
	WorkBid();
	void setWorkBid(int, Work, double);
	void displayWorkBid();
	int operator<(WorkBid&);
};


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
#include "Work.h"


Work::Work(void)
{
}

void Work::setWork(int num, double hour, double rates)
{
	worknum = num;
	workhour = hour;
	rate = rates;
}

double Work::getbaseprice()
{
	double baseprice = workhour * rate;

	return baseprice;
}

void Work::display()
{
	cout << "Work number " << worknum << endl;
	cout << "Work hour " << workhour << endl;
	cout << "Rate " << rate << endl;
	cout << "The baseprice is: $" << getbaseprice() << endl;
}


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
#include "WorkBid.h"


WorkBid::WorkBid(void)
{
}

void WorkBid::setWorkBid(int bidid, Work bid, double quote)
{
	bidnum = bidid;
	bidwork = bid;
	quotation = quote;
}

void WorkBid::displayWorkBid()
{
	cout << "Bid number " << bidnum << endl;
	cout << "Quotation " << quotation << endl;
	display();
	cout << endl;
}

int WorkBid::operator < (WorkBid& quote)
{
	if (quotation < getbaseprice())
	{return 1;}

	else
	return 0;
}


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
#include "Work.h"
#include "WorkBid.h"

int main()
{
	WorkBid work2;
	Work work1;
	int n, bids, i;
	double h, r, quote, baseprice;
	char stop = 'A';

	while (toupper(stop)!='S')
	{

		cout << "Enter Work number ";
		cin >> n;
		cout << "Enter Work hour ";
		cin >> h;
		cout << "Enter Rate ";
		cin >> r;

		work1.setWork(n, h, r);
		baseprice = work1.getbaseprice();
	
		cout << "Enter number of Bids ";
		cin >> bids;
		WorkBid* work2 = new WorkBid[bids];

		for (i=1; i < bids+1; i++)
		{
			cout << "Enter Quotation " ;
			cin >> quote;
			work2[i].setWorkBid(i, work1, quote); 
			work2[i].displayWorkBid();
		}

		cout << "S to stop ";
		cin >> stop;

	}
}
Last edited on Oct 6, 2013 at 2:48pm
Oct 6, 2013 at 2:58pm
Make work bid to not inherit from work and change line 19 in WorkBid.cpp:
display();bidwork.display();
Oct 6, 2013 at 3:02pm
WOW!!! it worked!

What about line 33? Am I setting in the values correctly?

How about my operator part?
Is it correct? How should I call out the operator in the int main()?

Thank you so much MiiNiPaa!
Last edited on Oct 6, 2013 at 3:05pm
Oct 6, 2013 at 3:05pm
What should that operator do?
Oct 6, 2013 at 3:07pm
What about line 33? Am I setting in the values correctly?

The operator will determine the quote.
If quote is more than the baseprice, it returns a 1, else 0.
Located in WorkBid.cpp last part
Last edited on Oct 6, 2013 at 3:08pm
Oct 6, 2013 at 3:21pm
You do not need an operator for that. Operator< should compare two different variable by specific parameter. You should make this another member function like display and setBid.

And probably it should return bool, not int.
Topic archived. No new replies allowed.