There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 freeze game hard drive internet internet explorer itunes laptop lcd linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition printer problem router slow software sound startup trojan usb virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: C++: Copy constructors for embedded clases


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
InterKnight's Avatar
Computer Specs
Senior Member with 288 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
12-May-2005, 05:54 PM #1
Solved: C++: Copy constructors for embedded clases
I hope that I can explain this without making a mess of my words.

I have a program that has a Date class and an Employee class. There are two embedded Date data members in the Employee class for Hire Date (hDate) and Birth Date (bDate). Here is my employee class definition:

Code:
class Employee
{
     friend istream& operator>>(istream&,Employee&);
     friend ostream& operator<<(ostream&,Employee&);
public:
     Employee(char* = "John",char* = "Doe",int = 123456789,
	int = 1,int = 1,int = 2000,int = 1,int = 1,int = 1980);
private:
     char fName[25];
     char lName[25];
     int ssn;
     Date hDate;
     Date bDate;
};
Now, let's say I create a copy constructor for the purposes of creating an object by using the new keyword (which is the requirement for this assignment). Here si what I came up with so far...

Code:
Employee::Employee(const Employee &e)
{
     strcpy(fName,e.fName);
     strcpy(lName,e.lName);
     ssn = e.ssn;
}
I also have the same type of constructor in the Date class definition. I also already overloaded the assignment operator (=) in the Date class. What do I need to do to implement the copy on the Date data memebers that are embedded in the Employee class? My compiler keeps saying "'Date', no default constructor available'". Is there a way to solve this problem?

Thanks for any help provided.

Please take care.
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
yto_daniel's Avatar
Senior Member with 216 posts.
 
Join Date: Mar 2005
Experience: Advanced
13-May-2005, 06:32 PM #2
Perhaps if you posted the date class itself it would give us a better idea why your getting that message.

Daniel - YourTechOnline.com technician
danielr@no_spam_yourtechonline.com (remove no_spam_)
InterKnight's Avatar
Computer Specs
Senior Member with 288 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
13-May-2005, 10:40 PM #3
I'm sorry about that. I should have just posted the entire program's code...I'll do that next time. Here is what I have, but unfortunately the copy constructor does not work...

Thanks for your help.

Code:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iomanip.h>
#include <conio.h>

class Employee;
class Date
{
     friend Employee;
     friend istream& operator>>(istream&,Date&);
     friend ostream& operator<<(ostream&,Date);
public:
     Date(int m,int d,int y) {setDate(m,d,y);}
     Date(const Date&);
     void setDate(int,int,int);
     Date& operator=(Date&);
     ~Date() {}
private:
     int month;
     int day;
     int year;
};

Date::Date(const Date &d)
{
     month = d.month;
     day = d.day;
     year = d.year;
}

void Date::setDate(int m,int d,int y)
{
     while(m < 1 || m > 12)
     {
	cout << endl << m << " is an invalid \"month\" entry.\n"
		<< endl << "Please re-enter month: ";
	cin >> m;
	cout << endl;
     }

     while(d < 1 || d > 30)
     {
	cout << endl << d << " is an invalid \"day\" entry.\n"
		<< endl << "Please re-enter day: ";
	cin >> d;
	cout << endl;
     }

     while(y < 1900 || y > 2010)
     {
	cout << endl << y << " is an invalid \"year\" entry.\n"
		<< endl << "Please re-enter year: ";
	cin >> y;
	cout << endl;
     }

     month = m;
     day = d;
     year = y;
}

Date& Date::operator=(Date &d)
{
     month = d.month;
     day = d.day;
     year = d.year;

     return *this;
}

istream& operator>>(istream& in,Date& d)
{
     in >> d.month;
     in.ignore();
     in >> d.day;
     in.ignore();
     in >> d.year;
     d.setDate(d.month,d.day,d.year);

     return in;
}

ostream& operator<<(ostream& out,Date d)
{
     out << d.month << "/" << d.day << "/" << d.year;

     return out;
}

class Employee
{
     friend istream& operator>>(istream&,Employee&);
     friend ostream& operator<<(ostream&,Employee&);
pulic:
     Employee(char* = "John",char* = "Doe",int = 123456789,
	int = 1,int = 1,int = 2000,int = 1,int = 1,int = 1980);
     Employee (const Employee&);
private:
     char fName[25];
     char lName[25];
     int ssn;
     Date hDate;
     Date bDate;
};

Employee::Employee(char* fn,char* ln,int ss,int hm,int hd,int hy,int bm, int   bd,int by)
: hDate(hm,hd,hy), bDate(bm,bd,by)
{
     strcpy(fName,fn);;
     strcpy(lName,ln);
     ssn = ss;
}

/*************************************************************
* This is where the compiler errors are generated from, reading:
* "'Date': no appropriate default constructor available" for each embedded Date object.
Employee::Employee(const Employee &e)
{
     strcpy(fName,e.fName);
     strcpy(lName,e.lName);
     ssn = e.ssn;
     Date::Date(e.hDate);
     Date::Date(e.bDate);
}
*************************************************************/

istream& operator>>(istream &in,Employee &e)
{
     cout << "First name: ";
     in >> e.fName;
     cout << "Last name: ";
     in >> e.lName;
     cout << "Social Security Number (SSN): ";
     in >> e.ssn;
     cout << "Hire date: ";
     cin >> e.hDate;
     cout << "Birth date: ";
     cin >> e.bDate;
	
     return in;
}

ostream& operator<<(ostream &out,Employee &e)
{
     out << "First name:\t" << e.fName << endl
	<< "Last name:\t" << e.lName << endl
	<< "SSN:\t\t" << e.ssn << endl;
     cout << "Hire date:\t" << e.hDate << endl
	<< "Birth date:\t" << e.bDate;;

     return out;
}

int main()
{
     Employee tst;

     cout << "Please enter the following personal information:\n\n";
     cin >> tst;

     cout << "Record information:\n\n" << tst << endl << endl;

     return 0;
}
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
15-May-2005, 02:35 PM #4
^^

Now that we have the full code, maybe we can figure something out.

If you need a speedier, super-detailed, nit-picky answer, ask at
http://episteme.arstechnica.com/eve/ubb.x in the Programmer's Symposium.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
15-May-2005, 02:51 PM #5
This compiles for me. You mispelled public in your employee class and in the date class, I changed 'friend Employee' to 'friend class Employee'.

Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <iomanip>

using namespace std;

class Date {
    friend class Employee;
    friend istream& operator>>(istream&,Date&);
    friend ostream& operator<<(ostream&,Date);
public:
    Date(int m,int d,int y) {setDate(m,d,y);}
    Date(const Date&);
    void setDate(int,int,int);
    Date& operator=(Date&);
    ~Date() {}
private:
    int month;
    int day;
    int year;
};

Date::Date(const Date &d) {
    month = d.month;
    day = d.day;
    year = d.year;
}

void Date::setDate(int m,int d,int y) {
     while(m < 1 || m > 12) {
        cout << endl << m << " is an invalid \"month\" entry.\n"
             << endl << "Please re-enter month: ";
        cin >> m;
        cout << endl;
    }
    while(d < 1 || d > 30) {
        cout << endl << d << " is an invalid \"day\" entry.\n"
             << endl << "Please re-enter day: ";
        cin >> d;
        cout << endl;
    }
    while(y < 1900 || y > 2010) {
        cout << endl << y << " is an invalid \"year\" entry.\n"
             << endl << "Please re-enter year: ";
        cin >> y;
        cout << endl;
    }
    month = m;
    day = d;
    year = y;
}

Date& Date::operator=(Date &d) {
    month = d.month;
    day = d.day;
    year = d.year;
    return *this;
}

istream& operator>>(istream& in,Date& d) {
    in >> d.month;
    in.ignore();
    in >> d.day;
    in.ignore();
    in >> d.year;
    d.setDate(d.month,d.day,d.year);
    return in;
}

ostream& operator<<(ostream& out,Date d) {
    out << d.month << "/" << d.day << "/" << d.year;
    return out;
}

class Employee {
    friend istream& operator>>(istream&,Employee&);
    friend ostream& operator<<(ostream&,Employee&);
public:
    Employee(char* = "John",char* = "Doe",int = 123456789,
    int = 1,int = 1,int = 2000,int = 1,int = 1,int = 1980);
    Employee (const Employee&);
private:
    char fName[25];
    char lName[25];
    int ssn;
    Date hDate;
    Date bDate;
};

Employee::Employee(char* fn,char* ln,int ss,int hm,int hd,int hy,int bm, int   bd,int by)
: hDate(hm,hd,hy), bDate(bm,bd,by) {
    strcpy(fName,fn);;
    strcpy(lName,ln);
    ssn = ss;
}

istream& operator>>(istream &in,Employee &e) {
    cout << "First name: ";
    in >> e.fName;
    cout << "Last name: ";
    in >> e.lName;
    cout << "Social Security Number (SSN): ";
    in >> e.ssn;
    cout << "Hire date: ";
    cin >> e.hDate;
    cout << "Birth date: ";
    cin >> e.bDate;
    return in;
}

ostream& operator<<(ostream &out,Employee &e) {
    out << "First name:\t" << e.fName << endl
        << "Last name:\t" << e.lName << endl
        << "SSN:\t\t" << e.ssn << endl;
   cout << "Hire date:\t" << e.hDate << endl
        << "Birth date:\t" << e.bDate;;
     return out;
}

int main() {
    Employee tst;
    cout << "Please enter the following personal information:\n\n";
    cin >> tst;
    cout << "Record information:\n\n" << tst << endl << endl;
}
Not sure if the program now does what you want, but for me it now compiles with no warnings or errors.
InterKnight's Avatar
Computer Specs
Senior Member with 288 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
15-May-2005, 03:21 PM #6
I tried recompiling after the edits, but I am still getting the same error. Basically, I need to get the Date objects to copy when the Employee copy constructor is called to allocate memory, and for some reason it is not working. I'm not sure exactly what the problem could be. Could it be the compiler itself?

Thanks very much for the help.
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
InterKnight's Avatar
Computer Specs
Senior Member with 288 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
15-May-2005, 03:45 PM #7
I took out the constructor "Employee(const Employee&)" and tried this code in main()

Employee *tst = new Employee;

And everything seemed to work flawlessly. The professor made a point in his class, and in his book, that to do such an allocation, you have to have the constructor with the const class object. When I tried adding it, I received the errors posted earlier for both of my Date objects.

It looks like the problem is solved, so I can finally now do the second part of it.

Thanks again for the help. I am definitely glad that my uncle recommended this site to me because everyone here is a great help to me.

Thanks again.
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:20 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.