| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1618 人关注过本帖
标题:第一次使用论坛有点小激动,最近在学C++,类和对象这段代码有点看不懂
只看楼主 加入收藏
joelllleino
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-5-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
第一次使用论坛有点小激动,最近在学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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
看到 #include <iostream.h>,有点儿历史了。它在C++历史上短暂存在过几年,上个世纪就被废弃。

假如你换用当代人用的编译器,我给你个示例
程序代码:
#include <iostream>
#include <string>
#include <string_view>

class person
{
public:
    person() = default;
    person( std::string_view vname, std::string_view vid, bool visman=true ) : name_(vname), id_(vid), isman_(visman)
    {
    }
    virtual ~person() = default;

    const std::string name() const
    {
        return name_;
    }
    void name( std::string_view vname )
    {
        name_ = vname;
    }

    const std::string id() const
    {
        return id_;
    }
    void id( std::string_view vid )
    {
        id_ = vid;
    }

    const bool isman() const
    {
        return isman_;
    }
    void isman( bool visman )
    {
        isman_ = visman;
    }

    friend std::ostream& operator<<( std::ostream& os, const person& obj )
    {
        os << "姓名:" << obj.name_ << ", "
           << "编号:" << obj.id_ << ", "
           << "性别:" << (obj.isman_?"":"");
        return os;
    }

private:
    std::string name_;
    std::string id_;
    bool isman_;
};

class student : public person
{
public:
    student( std::string_view vname, std::string_view vid, bool visman=true ) : person(vname,vid,visman)
    {
    }
    virtual ~student() = default;

    void score( double scorel, double score2, double score3 )
    {
        score_[0] = scorel;
        score_[1] = score2;
        score_[2] = score3;
    }
    friend std::ostream& operator<<( std::ostream& os, const student& obj )
    {
        os << static_cast<const person&>(obj) << "; 成绩:";
        for( const auto& sc : obj.score_ )
            os << ' ' << sc;
        return os;
    }

private:
    double score_[3] = { 100, 100, 100 };
};

class teacher : public person
{
public:
    teacher( std::string_view vname, std::string_view vid, bool visman=true, unsigned age=20 ) : person(vname,vid,visman), age_(age)
    {
    }
    virtual ~teacher() = default;

    friend std::ostream& operator<<( std::ostream& os, const teacher& obj )
    {
        os << static_cast<const person&>(obj) << "; 教龄:" << obj.age_;
        return os;
    }

private:
    unsigned age_;
};

int main( void )
{
    using namespace std;

    student stu( "joe", "01217", true );
    cout << stu << endl;

    stu.name( "Mary" );
    stu.id( "123456789" );
    stu.isman( false );
    stu.score( 80, 90, 85 );
    cout << stu << endl;

    teacher tea( "", "unknow", true );
    cout << tea << endl;

    tea.id( "不知道" );
    cout << tea << endl;
}


2020-05-30 20:48
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
我到现在才明白你想问什么,
  第一,你的代码要排版,不要让别人看天书
  第二,与问题不相干的代码就别贴出来,不要让别人大海捞针去寻找你的问题。
假如我来提问的话,我将会这么问:
#include <iostream>
#include <string>
using namespace std;

struct base
{
    base() {}
    base( const char* name ) : value(name) {}

    std::string value;
};

struct derived1 : base
{
    derived1( const char* name ) : member(name) {}

    base member;
};

struct derived2 : base
{
    derived2( const char* name ) : base(name) {}

    base member;
};

int main( void )
{
    derived1 d1( "joe" );
    cout << d1.value << endl; // 输出 空
    cout << d1.member.value << endl; // 输出 joe

    derived2 d2( "joe" );
    cout << d2.value << endl; // 输出 joe
    cout << d2.member.value << endl; // 输出 空
}

请问,为什么 d1.value 为空,为什么 d2.member.value 为空?

因为,
derived1 中,其基类base调用的是默认构造,其基类base的默认构造函数中并没有给 base::value 赋值"joe";
derived2 中,其成员member调用的是默认构造,其成员member的默认构造函数中并没有给 base::value 赋值"joe"。
2020-05-31 21:28
快速回复:第一次使用论坛有点小激动,最近在学C++,类和对象这段代码有点看不懂
数据加载中...
 
   



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

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