Thread: help with my class-composition, i am noob

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    help with my class-composition, i am noob

    //Person.cpp
    Code:
    #include <iostream>
    #include "Birth.h"
    #include "Person.h"
    
    using namespace std;
    
    void Person::setNumber(int day, int month, int year, int num)
    {
    	dob.setDay(day);
    	dob.setMonth(month);
    	dob.setYear(year);
    	number = num;
    }
    
    int Person::getNumber() const
    {
    	return number;
    }
    void Person::print() const
    {
    	dob.print();
    	cout << "Your number is " << number << endl;
    }
    
     Person::Person(int num):dob(day, month, year)
    {
    	number = 0;
    }
    //person.h
    Code:
    #include "Birth.h"
    
    #ifndef PERSON_H_
    #define PERSON_H_
    class Person
    {
    public:
    	void setNumber(int, int, int, int);
    	int getNumber()const;
    	void print()const;
    	Person();
    private:
    	int number;
    	Birth dob;
    };
    
    
    
    
    #endif
    //Birth.cpp
    Code:
    #include <iostream>
    #include "Birth.h"
    
    using namespace std;
    
    void Birth::setDay(int d)
    {
    	day = d;
    }
    
    int Birth::getDay() const
    {
    	return day;
    }
    
    void Birth::setMonth(int m)
    {
    	month = m;
    }
    
    int Birth::getMonth() const
    {
    	return month;
    }
    
    void Birth::setYear(int y)
    {
    	year = y;
    }
    
    int Birth::getYear() const
    {
    	return year;
    }
    
    void Birth::print()const
    {
    	cout << "Your dob is " << year << " " << month << " " << day << endl;
    }
    
    Birth::Birth()
    {
    	day = 0;
    	month = 0;
    	year = 0;
    }
    //Birth.h
    //Birth.h
    Code:
    #ifndef BIRTH_H_
    #define BIRTH_H_
    
    class Birth
    {
    public:
    	void setDay(int);
    	int getDay()const;
    	void setMonth(int);
    	int getMonth()const;
    	void setYear(int);
    	int getYear()const;
    	void print() const;
    	Birth();
    private:
    	int year;
    	int month; 
    	int day;
    };
    #endif
    main.cpp
    Code:
    #include <iostream>
    #include "Birth.h"
    #include "Person.h"
    
    using namespace std;
    
    void main(void)
    {
    	Birth a;
    	a.setDay(12);
    	a.setMonth(11);
    	a.setYear(2009);
    	a.print();
    	Person b;
    	b.setNumber(5);
    	b.print();
    	//a.setDay(12);
    	//a.setMonth(11);
    	//a.setYear(2009);
    	//a.print();
    	system ("pause");
    }
    I have no problem in anywhere expect the part where the and Person.cpp and Person.h which compostition from the birth.h and birth.cpp. What is wrong with my code, it not working. Is there anything I done wrong in person .cpp or person.h
    Last edited by evildotaing; 11-29-2011 at 09:27 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you sure the first 2 files aren't Person.cpp and Person.h? What's your question?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Change it , help me look where am I done wrong. The person.h and person.cpp got problem compile. No problem for other file

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by evildotaing View Post
    Change it , help me look where am I done wrong. The person.h and person.cpp got problem compile. No problem for other file
    Post the exact error message and the line number.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    I think person.cpp from line 7 onwards to the end where I think got problem. Person.h should be ok to me.
    1>------ Build started: Project: practice 4, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>e:\dm2121 advanced c++ programming\practice 4\practice 4\main.cpp(15) : error C2660: 'Person::setNumber' : function does not take 1 arguments
    1>Person.cpp
    1>e:\dm2121 advanced c++ programming\practice 4\practice 4\person.cpp(26) : error C2511: 'Person::Person(int)' : overloaded member function not found in 'Person'
    1> e:\dm2121 advanced c++ programming\practice 4\practice 4\person.h(7) : see declaration of 'Person'
    1>e:\dm2121 advanced c++ programming\practice 4\practice 4\person.cpp(30) : fatal error C1004: unexpected end-of-file found
    1>Generating Code...
    The error I received. I had tried to solve it but I am a bit confused in composition. I think composition is more harder than inhertiance.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look at this:
    Code:
    (15) : error C2660: 'Person::setNumber' : function does not take 1 arguments
    In other words, you declared Person::setNumber like this:
    Code:
    void setNumber(int, int, int, int);
    But you used it like this:
    Code:
    b.setNumber(5);
    Now look at this:
    Code:
    (26) : error C2511: 'Person::Person(int)' : overloaded member function not found in 'Person'
    In other words, you declared Person constructor as:
    Code:
    Person();
    But you implemented it as:
    Code:
    Person::Person(int num)
    Quote Originally Posted by evildotaing
    I had tried to solve it but I am a bit confused in composition. I think composition is more harder than inhertiance.
    This has nothing to do with composition versus inheritance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    ok, i know what is my first problem . I only give 1 number instead of 4 numbers in void setNumber(int, int, int, int).
    As for this part, what shout i put in the constructor header file? I not really sure but I get the point that I implement with a int while I do not have any int in header file in Person().
    Code:
     
     Person::Person(int num):dob(day, month, year) //in cpp file
    {
        number = 0;
    }
    because i add this code "dob(day, month, year)" in cpp file and I learn to write like this from a web source. But not sure about header file what to write>

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is SetNumber supposed to do? It should obviously accept whatever information it needs and no more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Noob Class Confusion
    By Freestyler in forum C++ Programming
    Replies: 16
    Last Post: 06-11-2009, 03:19 AM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  4. NOOB class question
    By wart101 in forum C++ Programming
    Replies: 38
    Last Post: 01-12-2007, 12:29 AM
  5. noob help on queue class
    By robaster in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 10:20 AM