| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1663 人关注过本帖
标题:第一次使用论坛有点小激动,最近在学C++,类和对象这段代码有点看不懂
取消只看楼主 加入收藏
joelllleino
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-5-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
第一次使用论坛有点小激动,最近在学C++,类和对象这段代码有点看不懂
#include <string.h>
#include <iostream.h>

class Cperson
{
public:
Cperson()
{
  strcpy(pName,"" );  //复制字符串到 pName 中
  strcpy(pID,"" );   //复制字符串到 pID 中
}
Cperson(char *name,  char *id, bool isman = 1)
{
    Input(name, id, isman);
}

void Input(char *name, char *id, bool isman)
{
    setName(name);
    setID(id);
    setSex(isman);
}

void Output( )
{
  cout<<"姓名:"<<pName<<endl;
  cout<<"编号:"<<pID<<endl;
  char *str=bMan?"男":"女";
  cout<<"性别:"<<str<<endl;
}
 
public:
//姓名属性操作
char *getName()const
{return(char*)pName;}
void setName(char *name)
{
  int n = strlen(name);
  strncpy(pName, name, n);
  pName[n] = '\0';
}
//编号属性操作
char *getID()const
{return(char*)pID;}
void setID(char *id)
{
  int n = strlen(id);
  strncpy(pID, id, n);
  pID[n] = '\0';
}
//性别属性操作
bool getSex() {return bMan;}
void setSex (bool isman) {bMan = isman;}
private:
char pName[20];   // 姓名
char pID[20];    // 编号
bool bMan;    // 性别:0 表示女,1 表示男
};

class Cstudent: public Cperson
{
public:
    Cstudent(char *name, char *id, bool isman = 1);
    ~Cstudent(){}
    void InputScore(double scorel, double score2, double score3);
    void Print();
    Cperson student;  // Cperson的对象成员变量
private:
    double dbScore[3];
     //三门成绩
};

class CTeacher: public Cperson
{
public:
    CTeacher(char *name, char *id, bool isman = 1, int years = 20);
    ~CTeacher(){}
    void Print();
private:
    int nTeachYears;           //教龄
};

#include <iostream.h>
#include "Ex_Class.h"
//类 CStudent 实现代码
Cstudent::Cstudent(char *name, char *id, bool isman)
    :student(name, id, isman)
{
 dbScore[0] = 100;
 dbScore[1] = 100;
 dbScore[2] = 100;
}
void Cstudent::InputScore(double scorel, double score2, double score3)
{
 dbScore[0] = scorel;
 dbScore[1] = score2;
 dbScore[2] = score3;
}
void Cstudent::Print()
{
    student.Output();
    for(int i=0; i<3; i++)
    cout<<"成绩"<<i+1<<":"<<dbScore[i]<<endl;
}
//类 Cteacher 实现代码
CTeacher::CTeacher(char *name, char *id, bool isman, int years)
{
    nTeachYears = years;
    Input(name, id, isman);
}
void CTeacher::Print()
{
    Output();
    cout<<"教龄"<<":"<<nTeachYears<<endl;
}
//主函数
void main()
{
    Cstudent stu("joe", "01217",1);
    cout<<stu.getName()<<endl;
    cout<<stu.student.getName()<<endl;
    stu.Print();
    stu.student.setName("Mary");
    stu.student.setSex(0);
    stu.student.setID("123456789");
    stu.InputScore(80, 90, 85);
    stu.Print();
    CTeacher tea("黄", "unknow",1);
    tea.Print();
    tea.setID("unknow");
    tea.Print();   
}

主要是对    cout<<stu.getName()<<endl和cout<<stu.student.getName()<<endl; ,这两句话有点不太清楚,希望大家解答
搜索更多相关主题的帖子: char void stu name bool 
2020-05-30 16:58
joelllleino
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-5-30
收藏
得分:0 
这个是我的理解,不知道对不对,希望得到大家纠正。对于stu.getName,这个stu对象直接去调用了Cperson中的getName,但是由于Cstudent中的构造函数没有具体的实现,所以stu中的名字无法被传入,所以什么都不输出
对于stu.student.getName,这里cpp文件前面中出现了对Cperson类中的student对象的一个初始化,所以使用stu.student相当于把stu中的参数传到了student中,相当于Cperson类中执行了那个有参的构造函数,这样把名字传到了pname中,然后.getName就可以输出名字
2020-05-30 17:04
joelllleino
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-5-30
收藏
得分:0 
回复 3楼 rjsp
#include <cstring>
#include <iostream>
using namespace std;
class Cperson
{private:
string pName;   // 姓名
string pID;    // 编号
bool bMan;    // 性别:0 表示女,1 表示男
public:
Cperson(){}


Cperson(string name,  string id, bool isman = true)
:pName(name),pID(id),bMan(isman)
{
   
}
void names(string name){pName=name;}
 string name(){return pName;}
     void ids(string id){pID=id;}
    string id(){return pID;}
void ismans(bool isman){bMan=isman;}
   bool isman(){return bMan;}
void Output( )
{
  cout<<"姓名:"<<pName<<endl;
  cout<<"编号:"<<pID<<endl;
 cout<<"性别"<< (bMan==true?"男":"女");
  
}
 const std::string getname(){return pName;}
 const std::string getid()const{return pID;}
bool getisman(){return bMan;}

};




class Cstudent: public Cperson
{private:
    double dbScore[3]={100,100,100};
public:
Cperson student;
    Cstudent(  string name, string id, bool isman = true
    )
    :Cperson(name,id,isman)
    {
   
    }
   
   
   
    ~Cstudent(){}
    void InputScore(double scorel, double score2, double score3)
    {
    dbScore[0] = scorel;
 dbScore[1] = score2;
 dbScore[2] = score3;
   
    }
    void Print()
    {Output();
        
    for(int i=0; i<3; i++)
    cout<<"成绩"<<i+1<<":"<<dbScore[i]<<endl;
   
    }
   

     //三门成绩
};

class CTeacher: public Cperson
{private:
    int nTeachYears;      
public:
    CTeacher(string name, string id, bool isman = true, int years = 20)
    :Cperson{name,id,isman},nTeachYears(years)
    {}
    ~CTeacher(){}
    void Print()
    { Output();
    cout<<"教龄"<<":"<<nTeachYears<<endl;   }
     //教龄
};


//类 Cteacher 实现代码

//主函数
int main()
{
    Cstudent stu("joe", "01217",true);
    cout<<stu.getname()<<endl;
   cout<<stu.student.getname()<<endl;
    stu.Print();
    stu.names("Marry");
    stu.ismans(false);
    stu.ids("123456789");
    stu.InputScore(80, 90, 85);
    stu.Print();
    CTeacher tea("黄", "unknow",1);
    tea.Print();
    tea.ids("unknow");
    tea.Print();   
}
版主请问一下,我之前的代码运行stu.getnme函数和stu.student.getname()函数时结果是第一个输出为空字符,第二个输出joe
然后又按照新编译器语法翻译一遍,发现两个函数出现了变化,变为joe ,然后空一行,
我想问继承中我原来的代码为什么是空一行呢,而且和新编译的代码区别这么大

2020-05-31 19:31
快速回复:第一次使用论坛有点小激动,最近在学C++,类和对象这段代码有点看不懂
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017579 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved