c++ class 初体验

来源:互联网 发布:e元素战隼z77宏编程 编辑:程序博客网 时间:2024/04/29 11:53

Visual Studio 2012

1. demo.cpp:------------------------------------#include <iostream>#include <stdlib.h>#include <string>#include "Student.h"using namespace std;/*****************************//*    class类的使用,初始化列表的使用*//*****************************/int main(){    Student stu;    /*cout<<stu.getName()<< " " <<stu.getScore()<<" "<<stu.getMax()<<endl;    stu.study();*/    Student stu1(stu);    return 0;}
2. Student.h:-----------------------------------#include <iostream>#include <string>using namespace std;class Student{public:    Student(string name = "jim", int score = 12, int m =100);    Student(const Student&stu); //拷贝构造函数   对象的复制    ~Student();//析构函数   释放资源    void setName(string _name);    string getName();    void setGender(string _gender);    string getGender();    void study();    int getScore();    int getMax();private:    string m_strName;    string m_strGender;    int m_iSocre;    const int m_iMax;//初始化列表};
3. Student.cpp:------------------------------------#include <iostream>#include <string>#include "Student.h"using namespace std;Student::Student(string name, int score, int m):m_strName(name),m_iSocre(score),m_iMax(m){    cout<<"Student(string name, int score)"<<endl;    //m_iMax = 180;}Student::Student(const Student &stu):m_iMax(1){    cout<<"Student(const Student &stu)"<<endl;}Student::~Student(){    cout<<"~Student()"<<endl;}void Student::setName(string _name){    m_strName = _name;}string Student::getName(){    return m_strName;}void Student::setGender(string _gender){    m_strGender = _gender;}string Student::getGender(){    return m_strGender;}void Student::study(){    cout<<"studing..."<<endl;}int Student::getScore(){    return m_iSocre;}int Student::getMax(){    return m_iMax;}
0 0
原创粉丝点击