473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

class composition problem

I am trying to increment a variable of a class with in another during
a recursive call to the main class. I know there are some problems
with the code below, I hope someone would point them out.

thanks
#include <iostream>
#include <string>
#include <vector>
#include <valarray>
#include <sstream>
#include <fstream>
#include <utility>

using namespace std;

class year
{
int yr;
public:
year(int);
void incur_yr();
int get_yr();
};
year::year(int n):yr(n){}
void year::incur_yr(){ yr++;}
int year::get_yr(){ return yr; }
class make
{
string mak;
year yr; // composition
public:
make(string, int);
void incurm_yr();
string get_make();
int get_yr();
};
make::make(string s, int n) : mak(s) { yr(n); }
void make::incurm_yr() { yr.incur_yr; }
string make::get_make(){ return mak; }
int make::get_yr(){ return yr.get_yr; }


int main(){
vector<makecars;
cars.push_back( car("GT", 1970) );
cars.push_back( car("chevy", 1957) );

for(unsigned i=0; i<cars.size(); i++)
for(j=0; j<2; j++)
if(cars[i].get_mak() == GT) {cars[i].incur_yr();}
cout << cars[0].get_yr();

//expected answer is 1772

}

Aug 28 '06 #1
7 1930
On Mon, 28 Aug 2006 14:11:27 +1000 in comp.lang.c++, Gary Wessle
<ph****@yahoo.comwrote,
>void make::incurm_yr() { yr.incur_yr; }
int make::get_yr(){ return yr.get_yr; }
Those are supposed to be function calls:

void make::incurm_yr() { yr.incur_yr(); }
int make::get_yr(){ return yr.get_yr(); }
^^

Aug 28 '06 #2
Hi David,
Those are supposed to be function calls:
Please don't answer homework questions. You might rather give an
encouraging tip than provide a solution he can just take.

Jens
Aug 28 '06 #3
Jens Theisen <jt***@arcor.dewrites:
Hi David,
>Those are supposed to be function calls:

Please don't answer homework questions. You might rather give an
encouraging tip than provide a solution he can just take.

Jens
I worked out most of the bugs but still getting the error;

try.cpp: In constructor 'make::make(std::string, int)':
try.cpp:46: error: no matching function for call to 'year::year()'
try.cpp:31: note: candidates are: year::year(int)
try.cpp:22: note: year::year(const year&)
make: *** [try.o] Error 1

************************************************** **************
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <valarray>
5 #include <sstream>
6 #include <fstream>
7 #include <utility>
8
9 using namespace std;
10
11
12
13 // void wait ( int seconds )
14 // {
15 // clock_t endwait;
16 // endwait = clock () + seconds * CLOCKS_PER_SEC;
17 // while (clock() < endwait) {}
18 // }
19
20
21 class year
22 {
23 int yr;
24 public:
25 year(int);
26 void incur_yr();
27 int get_yr();
28 void set_yr(int);
29
30 };
31 year::year(int n):yr(n){}
32 void year::incur_yr(){ yr++;}
33 int year::get_yr(){ return yr; }
34 void year::set_yr(int n){ yr = n;}
35
36 class make
37 {
38 string mak;
39 year yr; // composition
40 public:
41 make(string, int);
42 void incurm_yr();
43 string get_make();
44 int get_yr();
45 };
46 make::make(string s, int n) : mak(s) { yr.set_yr(n); }
47 void make::incurm_yr() { yr.incur_yr(); }
48 string make::get_make(){ return mak; }
49 int make::get_yr(){ return yr.get_yr(); }
50
51
52
53
54 int main(){
55
56
57 vector<makecars;
58 cars.push_back( make("GT", 1970) );
59 cars.push_back( make("chevy", 1957) );
60
61 for(unsigned i=0; i<cars.size(); i++)
62 for(int j=0; j<2; j++)
63 if(cars[i].get_make() == "GT") {cars[i].incurm_yr();}
64
65
66 cout << cars[0].get_yr();
67
68
69
70 //expected answer is 1772
71
72 // int n;
73 // cout << "Starting countdown...\n";
74 // for (n=10; n>0; n--)
75 // {
76 // cout << n << endl;
77 // wait (1);
78 // }
79 // cout << "left off" << '\n';
80
81 }

Aug 28 '06 #4
On Mon, 28 Aug 2006 09:17:43 +0100 in comp.lang.c++, Jens Theisen
<jt***@arcor.dewrote,
>Hi David,
>Those are supposed to be function calls:

Please don't answer homework questions. You might rather give an
encouraging tip than provide a solution he can just take.
No indication this was homework.

Even if it was, he posted his best effort code and asked for some
simple help. This is the kind of problem that you can look at for a
long time, in your own code, without spotting the problem. Nothing
like a second set of eyes.

The kind of "homework" that is ridiculed here is "Here is the
assignment give me the answer i need it today."

Aug 28 '06 #5
On Mon, 28 Aug 2006 20:10:15 +1000 in comp.lang.c++, Gary Wessle
<ph****@yahoo.comwrote,
>try.cpp: In constructor 'make::make(std::string, int)':
try.cpp:46: error: no matching function for call to 'year::year()'
try.cpp:31: note: candidates are: year::year(int)
try.cpp:22: note: year::year(const year&)
Since class year has user defined constructors, the compiler will
not supply a default constructor. Call the year constructor in the
initializer list of the constructor of class make.

See "# [10.6] Should my constructors use "initialization lists" or
"assignment"?" in Marshall Cline's C++ FAQ. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Aug 28 '06 #6
David Harmon wrote:
Even if it was, he posted his best effort code and asked for some
simple help.
My suspicion was that it was not his code, but the one contained in an
assignment; after all, the code is clearly not from a real world
problem. "I know there are some problems
with the code below" smelled like the wording of an assignment to me, in
the abscense of any error message or anything else.
The kind of "homework" that is ridiculed here is "Here is the
assignment give me the answer i need it today."
I didn't mean to ridicule anyone, sorry if I missed the tone.

Jens
Aug 28 '06 #7
Jens Theisen <jt***@arcor.dewrites:
David Harmon wrote:
>Even if it was, he posted his best effort code and asked for some
simple help.

My suspicion was that it was not his code, but the one contained in an
assignment; after all, the code is clearly not from a real world
problem. "I know there are some problems
with the code below" smelled like the wording of an assignment to me,
in the abscense of any error message or anything else.
the code was not a homework or an assignment, I made it up to the best
I can to understand a point I am having a problem with in a much
larger code, once I understand the point I try applying the correct
principle to my coding style.

>
>The kind of "homework" that is ridiculed here is "Here is the
assignment give me the answer i need it today."

I didn't mean to ridicule anyone, sorry if I missed the tone.

Jens
Aug 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bruce Lee Roy | last post by:
Hi, I am using visual studio.net and I am having problems using this bit of code when I create an instance, //////////////////////////////////////////// template<class T> class matrix : public...
17
by: Dave | last post by:
Hi I'm making a 3D Engine which consists of the class C3DEngine. Part of this engine is a file loader, a class called CMeshLoader. I have made an instance of CMeshLoader in C3DEngine, ie...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
6
by: JSheble | last post by:
I come from a Delphi background, and am currently trying to port or convert some of our Delphi classes to C#. I've got a good handle on basic class design, but am a bit lost with some of the more...
12
by: TS | last post by:
i have a question about this situation: i have a class that i use that i need to know 1 piece of data that exists in a different class. Im wondering is it wrong to add the property that i need...
1
by: tony | last post by:
Hello! I access the class MyComparer in this way. steel_post.Sort(new MeltPracDataComposition.Composition.MyComparer()); You can see the class definition for MyComparer below. As you can see...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
1
by: Jennifer Jazz | last post by:
My question is regarding the mapping of Class diagram to the C++ coding. There are 3 realtions in Class diagram 1) Assosication 2) Composition 3) Aggregation (Weak Composition). ...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.