C++ 代码多态性的错误结果

wrong result with c++ code polymorphism

本文关键字:错误 结果 多态性 代码 C++      更新时间:2023-10-16

我被要求编写一个程序,其中有 3 个类,一个是学生,第二个是日期,最后一个是地址我必须主要从班级学生创建一个对象,该对象在课程日期的 3 个日期和班级地址的地址旁边有班级学生的所有属性所以我编写了代码,并在类日期中使用了一个函数来确保日期的形式是正确的 m/d/y,我也使用了 set 和 get 函数但问题是当我编译代码时,最终结果将所有值显示为"1"

这是我的代码://类日期标题:

#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;
 }

类地址标头:

#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 ; 
}

问题是:[链接](http://www.4shared.com/photo/ik6C5EZFce/pro.html(

问题是你在构造函数末尾的打印:

使用 & 将获取数据的地址(这里甚至是您的成员函数的地址(,而不是数据本身。

将其更正为:

cout << "Student object constructor : " 
     << Student::getFirst_Name() << ' ' << Student::getLast_Name() << ' ' <<   Student::getPhone_Number() << ' ' <<  Student::getMobile_Number()  << ' ' << Student::getSpecialization()  <<  endl ; 
}