我被要求编写一个程序,其中有3个班级为学生,第二个为日期,最后一个对象是地址 我必须在班级学生的主要课程中创建一个对象,该班级学生除了课程日期的3个日期和班级地址的地址之外还有班级学生的所有属性 所以我写了代码,我在类日期中使用了一个函数,以确保日期是正确的m / d / y形式,我也使用了set和get函数 但问题是当我编译代码时,最终结果显示所有值为" 1"
这是我的代码:// class date header:
#ifndef Date_H
#define Date_H
class Date
{
public:
Date( int , int , int ); // default constructor
void setmonth(int &);
void setday(int &);
void setyear(int &);
int getmonth() const;
int getday() const;
int getyear() const;
void print() const; // print date in month/day/year format
~Date(); // provided to confirm destruction order
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int );
};
#endif
//班级日期来源:
#include <iostream>
using namespace std;
using std::cout ;
#include "Date.h"
// Constructor: Confirm proper value for month;
// call utility function checkDay to confirm proper
// value for day.
Date::Date( int mn, int dy, int yr )
{
if (mn > 0 && mn <= 12 ) // validate the month
month = mn ;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
month = mn ; // validate the month
year = yr; // should validate yr
day = checkDay( dy ); // validate the day
cout << "Date object constructor for date ";
print();
cout << endl;
}
int Date::getmonth() const
{
return month;
}
int Date::getday() const
{
return day;
}
int Date::getyear() const
{
return year;
}
void Date::setmonth(int &m)
{
month = m;
}
void Date::setday(int &d)
{
day = d;
}
void Date::setyear(int &y)
{
year = y;
}
// Print Date object in form month/day/year
void Date::print() const
{ cout <<&Date::getmonth << '/' << &Date::getday << '/' << &Date::getyear ; }
// Destructor: provided to confirm destruction order
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
}
// class Address header:
#ifndef ADDRESS_H
#define ADDRESS_H
#include <string>
using namespace std ;
class Address
{
public:
Address(double, string , string ,string ,double);// default constructor
double getbuilding_number() const;
string getstreet() const;
string getcity() const;
string getcountry() const;
double getcode() const;
void setbuilding_number(double &);
void setstreet(string &);
void setcity(string &);
void setcountry(string &);
void setcode(double &);
void print() const;
~Address(); // provided to confirm destruction order
private:
double building_number;
string street;
string city;
string country;
double code;
};
#endif
//类地址来源:
#include <iostream>
#include "Address.h"
using namespace std;
// Constructor
Address::Address( double b_n ,string st ,string ci ,string coun ,double cod )
{
building_number = b_n;
street = st;
city = ci;
country = coun;
code = cod;
cout << "Date object constructor for Address ";
print();
cout << endl;
}
void Address::setbuilding_number(double &b_n)
{
building_number =b_n;
}
void Address::setstreet(string &st)
{
street = st;
}
void Address::setcity(string &ci)
{
city= ci;
}
void Address::setcountry(string &co)
{
country = co;
}
void Address::setcode(double &co)
{
code = co;
}
double Address::getbuilding_number() const
{
return building_number;
}
string Address::getstreet() const
{
return street;
}
string Address::getcity() const
{
return city;
}
string Address::getcountry() const
{
return country;
}
double Address::getcode() const
{
return code;
}
// Print Adress object
void Address::print() const
{
cout <<" Student's building number is " << &Address::getbuilding_number << endl
<< " Student's street is " << &Address::getstreet << endl
<< " Student's city is " << &Address::getcity << endl
<< " city code is " << &Address::getcode << endl
<< " Student's country is " << &Address::getcountry << endl ;
}
// Destructor: provided to confirm destruction order
Address::~Address()
{
cout << "Address object destructor for Address ";
print();
cout << endl;
}
//班级学生头:
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std ;
#include "Address.h"
#include "Date.h"
class Student
{
public:
Student(string,string,string,string,string,int,int,int,int,int,int,int,int,int,double,string,string,string,double);
void setFirst_Name(string &);
void setLast_Name(string &);
void setPhone_Number(string &);
void setMobile_Number(string &);
void setBirth_Date(Date &);
void setCertificate_Date(Date &);
void setRegistration_Date(Date &);
void setSpecialization(string &);
void setStudent_Address(Address &);
string getFirst_Name() const;
string getLast_Name()const ;
string getPhone_Number() const;
string getMobile_Number() const;
Date getBirth_Date() const;
Date getCertificate_Date() const;
Date getRegistration_Date() const;
string getSpecialization() const;
Address getStudent_Address() const;
void print() const ;
~Student(); // provided to confirm destruction order
private:
string First_Name;
string Last_Name;
string Phone_Number;
string Mobile_Number;
Date Birth_Date;
Date Certificate_Date;
Date Registration_Date;
string Specialization ;
Address Student_Address;
};
#endif
//班级学生来源:
#include <iostream>
#include <string>
using namespace std;
using std::cout ;
#include "Student.h"
Student::Student(string F_Name , string L_Name , string Ph_Number , string Mob_Number ,string Spec ,
int B_Month , int B_Day , int B_year ,
int C_Month , int C_Day , int C_year ,
int R_Month , int R_Day , int R_year ,
double S_Building_Num , string S_Street , string S_City , string S_Country , double S_Code )
:Birth_Date( B_Month, B_Day, B_year ),
Certificate_Date( B_Month , B_Day, B_year ),
Registration_Date(B_Month , B_Day, B_year ),
Student_Address(S_Building_Num , S_Street , S_City , S_Country ,S_Code )
{
First_Name = F_Name ; // copy F_Name into First_Name
Last_Name = L_Name ; // copy L_Name into Last_Name
Phone_Number = Ph_Number ; // copy Ph_Number into Phone_Number
Mobile_Number = Mob_Number ; // copy Mob_Number into Mobile_Number
Specialization = Spec ; // copy Spec into Specialization
cout << "Student object constructor : "
<< &Student::getFirst_Name << ' ' << &Student::getLast_Name << ' ' << &Student::getPhone_Number << ' ' << &Student::getMobile_Number << ' ' << &Student::getSpecialization << endl ;
}
void Student::setFirst_Name(string &fname)
{
First_Name = fname ;
}
void Student::setLast_Name(string &lname )
{
Last_Name = lname ;
}
void Student::setPhone_Number(string &PN)
{
Phone_Number = PN ;
}
void Student::setMobile_Number(string &MN)
{
Mobile_Number = MN ;
}
void Student::setBirth_Date(Date &birth )
{
Birth_Date = birth ;
}
void Student::setCertificate_Date(Date &cer)
{
Certificate_Date = cer ;
}
void Student::setRegistration_Date(Date &rdate)
{
Registration_Date = rdate ;
}
void Student::setSpecialization(string &spe)
{
Specialization = spe ;
}
void Student::setStudent_Address(Address &s_address)
{
Student_Address = s_address ;
}
string Student::getFirst_Name() const
{
return First_Name ;
}
string Student::getLast_Name() const
{
return Last_Name ;
}
string Student::getPhone_Number() const
{
return Phone_Number;
}
string Student::getMobile_Number() const
{
return Mobile_Number;
}
Date Student::getBirth_Date() const
{
return Birth_Date ;
}
Date Student::getCertificate_Date() const
{
return Certificate_Date;
}
Date Student::getRegistration_Date() const
{
return Registration_Date;
}
string Student::getSpecialization() const
{
return Specialization ;
}
Address Student::getStudent_Address() const
{
return Student_Address ;
}
void Student::print() const
{
cout <<&Student::getFirst_Name << " , " << &Student::getLast_Name << "," << &Student::getPhone_Number << " , " << &Student::getMobile_Number << "\nBorn on : " ;
Birth_Date.print() ;
cout << "The date of the Bakaloria Certificate is : " ;
Certificate_Date.print() ;
cout << "The date of the Registration is : " ;
Registration_Date.print() ;
cout << "Student's Specialization is : " << &Student::getSpecialization << endl ;
cout << "Student's Address is : " ;
Student_Address.print() ;
cout << endl ;
}
// Destructor: provided to confirm destruction order
Student::~Student()
{
cout << " Student object destructor : "
<< First_Name << ' ' << Last_Name << ' ' << Phone_Number << ' ' << Mobile_Number << ' ' << Specialization << endl ;
}
//主要功能:
#include <iostream>
#include <string>
using namespace std ;
#include "Student.h"
int main ()
{
Student s("dana" , "fakhri" , "25186" , "0940200", "ise" , 9, 9 ,1990, 9,15,2009 , 4,12,2011 , 6 , "AL-Jala2 street " , "Hamah" , "Syria" , +963 ) ;
cout << '\n' ;
s.print() ;
return 0 ;
}
答案 0 :(得分:0)
问题是你在构造函数的末尾打印:
使用&
将获取数据的地址(这里甚至是你的memeber函数),而不是数据本身。
纠正于:
cout << "Student object constructor : "
<< Student::getFirst_Name() << ' ' << Student::getLast_Name() << ' ' << Student::getPhone_Number() << ' ' << Student::getMobile_Number() << ' ' << Student::getSpecialization() << endl ;
}