我用基类Person
创建了一个继承类Person.h
#include <iostream>
#include <string>
using namespace std;
class Person
{
friend ostream& operator<<(ostream &os, const Person &p);
friend istream& operator>>(istream &is, Person &p);
private:
char* m_name;
int m_age;
string m_ssn;
protected:
virtual void Read(istream &is);
virtual void Write(ostream &os) const;
public:
Person();
Person(const char* const name, int age, const string &ssn);
Person(const Person &p);
~Person();
Person & operator = (const Person& other);
virtual string getName() const;
virtual int getAge() const;
virtual string getSSN() const;
void setName(const char* const name);
void setAge(int age);
void setSSN(string ssn);
string getType() const;
};
Person.cpp
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
ostream& operator<<(ostream &os, const Person &p)
{
p.Write(os);
return os;
}
istream& operator>>(istream &is, Person &p)
{
p.Read(is);
return is;
}
Person::Person()
{
m_name = new char[10];
for (int i = 0; i < 10-1; i++)
{
m_name[i] = 'X';
}
m_name[10 - 1] = '\0';
m_age = 0;
m_ssn = "XXX-XX-XXXX";
}
Person::Person(const char* name, int age, const string &ssn)
{
m_name = new char[(int)strlen(name)+1];
for (int y = 0; y < (int)strlen(name); y++)
m_name[y] = name[y];
m_age = age;
m_ssn = ssn;
}
Person::Person(const Person& P)
{
m_name = new char[(int)strlen(P.m_name)+1];
for (size_t i = 0; i <= (int)strlen(P.m_name); i++)
{
m_name[i] = P.m_name[i];
}
m_age = P.m_age;
m_ssn = P.m_ssn;
}
Person::~Person()
{
delete[] m_name;
}
Person& Person::operator=(const Person& other)
{
if (this == &other)
return *this;
delete [] m_name;
m_name = new char[(int)strlen(other.m_name) + 1];
for (int i = 0; i <= (int)strlen(other.m_name); i++)
{
m_name[i] = other.m_name[i];
}
m_age = other.m_age;
m_ssn = other.m_ssn;
}
string Person::getName() const
{
return string(m_name);
}
int Person::getAge() const
{
return int(m_age);
}
string Person::getSSN() const
{
return string(m_ssn);
}
void Person::setName(const char* const name)
{
delete[] m_name;
m_name = new char[(int)strlen(name) + 1];
for (int i = 0; i <= (int)strlen(name); i++)
{
m_name[i] = name[i];
}
}
void Person::setAge(int age)
{
m_age = age;
}
void Person::setSSN(string ssn)
{
m_ssn = ssn;
}
string Person::getType() const
{
return string("Person");
}
void Person::Write(ostream &os) const
{
if (&os == &cout)
{
os << "Name: " << m_name << "\nAge: " << m_age << "\nSSN: " << m_ssn;
}
else
os << m_name << "\n" << m_age << "\n" << m_ssn;
}
void Person::Read(istream &is)
{
string name;
if (&is == &cin)
{
cout << "Enter the Name: " << flush;
getline(is, name);
if (name.length() == 0)
getline(is, name);
cout << "Enter the Age: " << flush;
is >> m_age;
cout << "Enter the SSN: " << flush;
getline(is, m_ssn);
is.get();
}
else
{
getline(is, name);
if (!is)
return;
if (name.length() == 0)
getline(is, name);
is >> m_age;
getline(is, m_ssn);
is.get();
}
delete[] m_name;
m_name = new char[name.length() + 1];
for (int i = 0; i < (int)name.length(); i++)
{
m_name[i] = name[i];
}
m_name[name.length()] = '\0';
}
我甚至不会放入继承类,即Student,因为我在测试Person类时已经出错了。
我在我的主要内容中试过以下内容:
#include "Person.h"
#include "Student.h"
int main()
{
Person ptr2Stu("Joe", 22, "123-45-6678");
cout << ptr2Stu.getName() << endl;
cout << ptr2Stu.getAge() << endl;
cout << ptr2Stu.getSSN() << endl;
cout << ptr2Stu.getType() << endl;
system("pause");
}
尝试构建时会收到错误。
答案 0 :(得分:1)
Person.cpp
Person::Person(const char* name, int age, const string &ssn)
在头文件中,声明是
Person(const char* const name, int age, const string &ssn);
^^^^
因此,更改声明/定义中的任何一个。
您可以在getSSN
和getAge
中省略类型转换。