PDA

View Full Version : مبتدی: کلاسی از یک دانشجو



smemamian
جمعه 17 آذر 1391, 10:34 صبح
سلام دوستان

مشکل این کد چیه ؟

#include <cstdlib>
#include <iostream>

#define StudentsH
#define StudentsH
using namespace std;

class TStudent

{

public:

TStudent();

TStudent(string fn, string ln, int d, int m, int y);

~TStudent();

void setFirstName(string f) { FirstName = f; }

string getFirstName() { return FirstName; }

void setLastName(string l) { LastName = l; }

string getLastName() { return LastName; }

void setDayOfBirth(int d) { DayOfBirth = d; }

int getDayOfBirth() { return DayOfBirth; }

void setMonthOfBirth(int m) { MonthOfBirth = m; }

int getMonthOfBirth() { return MonthOfBirth; }

void setYearOfBirth(int y) { YearOfBirth = y; }

int getYearOfBirth() { return YearOfBirth; }



private:

string FirstName;

string LastName;

int DayOfBirth;

int MonthOfBirth;

int YearOfBirth;

};

int main(int argc, char *argv[])
{

string FN, LN;

int Day, Month, Year;

TStudent St;



cout << "Enter the student's information\n";

cout << "First Name: ";

cin >> FN;

cout << "Last Name: ";

cin >> LN;

cout << "Day of Birth: ";

cin >> Day;

cout << "Month of Birth: ";

cin >> Month;

cout << "Year of Birth: ";

cin >> Year;



St.setFirstName(FN);

St.setLastName(LN);

St.setDayOfBirth(Day);

St.setMonthOfBirth(Month);

St.setYearOfBirth(Year);



cout << "\nStudent Information";

cout << "\nFull Name: " << St.getFirstName()

<< " " << St.getLastName();

cout << "\nDate of Birth: " << St.getDayOfBirth()

<< "/" << St.getMonthOfBirth()

<< "/" << St.getYearOfBirth();

system("PAUSE");
return EXIT_SUCCESS;
}

omidshaman
جمعه 17 آذر 1391, 13:13 عصر
شما توی constructor مشکل دارین
وقتی شما تعریف می کنی

TStudent(string fn, string ln, int d, int m, int y);
خوب خط 63 هم باید این متغیر ها رو بفرستی به class
یعنی خط 63 باید باشه

TStudent St(FN,LN,Day,Month,Year);
نیازی هم به تعریف این همه فانکشن و این چیزا هم نیست constructor کارش همینه البته اون جوری که شما نوشتین هم درسته فقط باید همون خط 63 عوض بشه

#include <cstdlib>
#include <iostream>
using namespace std;
class TStudent
{
public:

TStudent(string FN,string LN,int Day,int Month,int Year) :FirstName(FN),
LastName(LN),
DayOfBirth(Day),
MonthOfBirth(Month),
YearOfBirth(Year){}

~TStudent(){}

void show()
{
cout << "\nStudent Information";
cout << "\nFull Name: " << FirstName<< " " << LastName;
cout << "\nDate of Birth: " << DayOfBirth<<"\\"<<MonthOfBirth<<"\\"<<YearOfBirth;
}

private:

string FirstName;
string LastName;
int DayOfBirth;
int MonthOfBirth;
int YearOfBirth;
};

int main(int argc, char *argv[])
{

string FN, LN;

int Day, Month, Year;

cout << "Enter the student's information\n";

cout << "First Name: ";

cin >> FN;

cout << "Last Name: ";

cin >> LN;

cout << "Day of Birth: ";

cin >> Day;

cout << "Month of Birth: ";

cin >> Month;

cout << "Year of Birth: ";

cin >> Year;

TStudent St(FN,LN,Day,Month,Year);
St.show();

system("PAUSE");
return EXIT_SUCCESS;
}

smemamian
جمعه 17 آذر 1391, 14:07 عصر
خیلی ممنونم مشکل حل شد .