#include <iostream>
#include <cstring>

using namespace std;

class date
{
public:
   date(int = 1, int = 1, int = 1990); //constructor
   void Printdate();

   void setday(int);
   void setmonth(int);
   void setyear(int);
   void setdate(int, int, int);

private:
   int day, month, year;

};

date::date(int d, int m, int y)
{
setdate(d,m,y);
}


void date::setdate(int dd, int mm , int yy )
{
day = dd;
month = mm;
year = yy;
}

void date::Printdate()
{
cout << day << "/" << month << "/" << year  <<endl;
}

void date::setday(int dd)
{
day = dd;
}

void date::setmonth(int mm)
{
month = mm;
}

void date::setyear(int yy)
{
year = yy;
}

class Student
{
public:
   Student(); //constuctor for student class
   void setName(char );
   void PrintName();

   date Printdate();

private:
   char student[20]; // array to store name.
};

Student::Student()
{
strcpy(student, "Acidburn");
}

void Student::PrintName()
{
cout << student<<endl;
}

void Student::setName(char N)
{
strcpy(student , " N ");
}

int main(void)
{

int day = 0, month = 0, year = 0;
char student[20]; // array to hold students name.

date date1;

Student St1;

cout << "Before call to set day" << endl;

date1.Printdate(); //shows default values from consturctor

cout << " ----------------------------------------------------\n";

cout << "Please enter your student details" <<endl;

cin >> student;

St1.setName("student");



return 0;
}

Now the problem is with the student class ... In main I've declared an array of char student[20]; . I ask the user to enter the name in main. Anyway on passing the student information to the class to write to the private array data. I get an error

D:\extra programming\temp\class.cpp(103) : error C2664: 'setName' : cannot convert parameter 1 from 'char [8]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

Can anyone point me in the direction for fixing it?

Thanks

I am not entirely sure at all, but I don't think you can use cin to populate an array of characters. I think you might want to use cin.getline()

Upon further inspection of your comment I decided to delete the St1.setName("student"); and replace it with a cout << student - Prints the value of the array.

It compiles and runs... But doesnt do what I want it to do. But it does populate the array :D , Basically I would like to the user to enter a name from the UI and then that name gets passed to the function setName - which then adds / edits the array in the private data class.

lol wicked... strange I was going to try pointers but not to worry. Thanks for the post, I'm left with one more question.

public: 
   Student(); //constuctor for student class 
   void setName(char ); 
   void PrintName(); 

   date Printdate(); 

private: 
   char student[20]; // array to store name. 
};

has you can see I'm trying to call a function from another class, but it doesnt want to work. I guess I can do it using inhertiance but I'm sure you can do it this way. Or at least something similar??

#include <iostream>
#include <cstring>

using namespace std;

class date
{
public:
   date(int = 1, int = 1, int = 1990); //constructor
   void Printdate();

   void setday(int);
   void setmonth(int);
   void setyear(int);
   void setdate(int, int, int);

private:
   int day, month, year;

};

date::date(int d, int m, int y)
{
setdate(d,m,y);
}


void date::setdate(int dd, int mm , int yy )
{
day = dd;
month = mm;
year = yy;
}

void date::Printdate()
{
cout << day << "/" << month << "/" << year  <<endl;
}

void date::setday(int dd)
{
day = dd;
}

void date::setmonth(int mm)
{
month = mm;
}

void date::setyear(int yy)
{
year = yy;
}

class Student
{
public:
   Student(); //constuctor for student class
   void setName(char );
   void PrintName();

   date Printdate();

private:
   char student[20]; // array to store name.
};

Student::Student()
{
strcpy(student, "Acidburn");
}

void Student::PrintName()
{
cout << student<<endl;
}

void Student::setName(char N)
{
strcpy(student , " N ");
}

int main(void)
{

int day = 0, month = 0, year = 0;
char student[20]; // array to hold students name.

date date1;

Student St1;

cout << "Before call to set day" << endl;

date1.Printdate(); //shows default values from consturctor

cout << " ----------------------------------------------------\n";

cout << "Please enter your student details" <<endl;

cin >> student;

St1.setName("student");



return 0;
}

Now the problem is with the student class ... In main I've declared an array of char student[20]; . I ask the user to enter the name in main. Anyway on passing the student information to the class to write to the private array data. I get an error

Can anyone point me in the direction for fixing it?

Thanks

Hello,
You have a couple of design problems. Let me point them out.

in your main you are passing a string literal(or constant) to the setName
function. You got the name from user and stored it in student. Now pass
the student variable to setName.

cout << "Please enter your student details" <<endl;

cin >> student;

St1.setName("student"); ** Remove the quotes- St1.setName(student);

Then in your setName function, you were passing a string literal ("student"), that was probably converted to a char{] by the compiler, and was trying to assign it to a single char (char N) in the function definition. strcpy takes char[] (char arrays) if I'm not mistaken, there would be no reason to use it for a char, just copy one char to another.

void Student::setName(char N) // change to setName(char N[])
{
strcpy(student , " N "); // change to strcpy(student, N);
} // "N" is NOT char N, it is a char literal

Then in your class definition, you are passing a char in. Change it to char[].

class Student
{
public:
Student(); //constuctor for student class
void setName(char ); // void setName(char[]);
void PrintName();

date Printdate();

private:
char student[20]; // array to store name.
};

Basically you were trying to use a char instead of a char array[], big difference for what you were trying to do. If you only wanted one char, then that would be fine.

Hope this helped.

I am not entirely sure at all, but I don't think you can use cin to populate an array of characters. I think you might want to use cin.getline()

Hi Dani,

Actually you can use cin to get a char[], but you can go out of bounds on the char[] if the user typed more than was allocated. You get an out of bounds debug error. But you are correct that cin.getline is a better and safer way.

Cheers

Radioman28 :cool:

lol wicked... strange I was going to try pointers but not to worry. Thanks for the post, I'm left with one more question.

public: 
   Student(); //constuctor for student class 
   void setName(char ); 
   void PrintName(); 

   date Printdate(); 

private: 
   char student[20]; // array to store name. 
};

has you can see I'm trying to call a function from another class, but it doesnt want to work. I guess I can do it using inhertiance but I'm sure you can do it this way. Or at least something similar??

What you are trying to do is composition. Copmposition is a "has a" relationship. Class Student "has an" object of class date. But you are declaring it wrong. Try this:

date newDate;

That creates a date object called newDate which is a member of class Student.

In order to use it, you call it like this in main:

St1.newDate.Printdate();

Now this is NOT a good way of using classes. date newDate is a public member of class Student (you have it listed as date Printdate()), with public accesible functions. It would be better that newDate become a private field and write GET/SET's for it. Although, it works as I showed.

Cheers :cool:

ok in the Date class I've included the set and get functions : Heres the editied code:

#include <iostream>
#include <cstring>

using namespace std;

class date
{
public:
   date(int = 1, int = 1, int = 1990); //constructor
   void Printdate();

   void setday(int);
   void setmonth(int);
   void setyear(int);
   void setdate();

   int getday();
   int getmonth();
   int getyear();

private:
   int day, month, year;

};

date::date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}


void date::setdate ()
{
   int dd = 0, mm = 0, yy = 0;

   cin >> dd >> mm >> yy;

      day = dd;
      month = mm;
      year = yy;
  }   

void date::Printdate()
{
cout << day << "/" << month << "/" << year  <<endl;
}

void date::setday(int dd)
{
day = dd;
}

void date::setmonth(int mm)
{
month = mm;
}

void date::setyear(int yy)
{
year = yy;
}

int date::getday()
{
   return day;
}

int date::getmonth()
{
   return month;
}

int date::getyear()
{
   return year;
}


class Student
{
public:
   Student(); //constuctor for student class
   void setName(void);
   void PrintName();

   date Printdate();

private:
   char student[20]; // array to store name.
};

Student::Student()
{
   cout << "default name created : " ;
strcpy(student, "Acidburn");
}

void Student::PrintName()
{
cout << student<<endl;
}

//void Student::setName(char *N)
void Student::setName()
{
   char name[20];
   cin >> name;
   strcpy(student ,  name );
}

int main(void)
{

date date1;

Student St1;

cout << "Before call to set day" << endl;

date1.Printdate(); //shows default values from consturctor

St1.PrintName();

cout << " ----------------------------------------------------\n";

cout << "Please enter your student details" <<endl;


St1.setName();

St1.PrintName();

cout << "Now enter there date of birth (D.o.B)" << endl;

date1.setdate();

date1.Printdate();

return 0;
}

could someone explain why we need to use the set and get functions ? So far using the get function makes no sence to me... only adding to the size of the program??

ok - No edit!! :( !! Anyway I think I've managed to understand my set and get functions. I've sorted that one for now. Back to the "As a and the other relationship". I've got this so far in main():

date newDate;

St1.Printdate();

However I get an error

Linking...
class.obj : error LNK2001: unresolved external symbol "public: class date __thiscall Student::Printdate(void)" (?Printdate@Student@@QAE?AVdate@@XZ)
Debug/class.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I tried what you said:

date newDate;

St1.newDate.Printdate();

and I got this error:

Compiling...
class.cpp
D:\extra programming\temp\class.cpp(140) : error C2039: 'newDate' : is not a member of 'Student'
D:\extra programming\temp\class.cpp(82) : see declaration of 'Student'
D:\extra programming\temp\class.cpp(140) : error C2228: left of '.Printdate' must have class/struct/union type
Error executing cl.exe.

anyone care to explain this? I'm out of ideas

This seems pretty clear to me.

D:\extra programming\temp\class.cpp(140) : error C2039: 'newDate' : is not a member of 'Student'
D:\extra programming\temp\class.cpp(82) : see declaration of 'Student'

You never wrote this function.

class.obj : error LNK2001: unresolved external symbol "public: class date __thiscall Student::Printdate(void)"

I'm confused on how you would go back writing the function for it? I thought it should just call that function that belongs to the other class?

haha. I think I may have it

#include <iostream>

using namespace std;

class Date
{
public:
   Date(int = 1, int = 1 , int = 1900);
   void print();
  

private:
   int month, day, year;

};

Date::Date(int dy, int mn, int yr)
{
month = mn;
day = dy;
year = yr;

cout << "object created " << endl;

print();
}

void Date::print()
{
   cout << day << "/" << month << "/" << year << endl;
}

class Student
{
public:
   Student(const char *, const char *, const Date&);
   void print()const;

private:

   char firstname[25];
   char lastname[25];

   const Date birthdate;
};

Student::Student(const char *first, const char *last, const Date &dateOfbirth):birthdate(dateOfbirth)
{
strcpy(firstname ,  first ); 
strcpy(lastname, last);
}

void Student::print()const
{
   cout << firstname << " " << lastname << endl;
}

int main()
{
Date birth(7,24,1949);
Student student1("John", "simth", birth);
student1.print();
   return 0;
}

after search and playing it works :D

This article has been dead for over six months. Start a new discussion instead.