#include <QCoreApplication>
#include <iostream>
using namespace std;
/*struct*/class Student{//结构体成员默认public,类成员默认private
//public:
// string m_name;
// int m_age;
public:
//构造函数
Student (string const& name, int age){
m_name = name;
m_age = age;
}
//成员函数
void setName(string const& name){
if(name != "威震天")
m_name = name;
}
void setAge(int age){
if(age > 0)
m_age = age;
}
void who(void){
cout << "我是 " << m_name << endl;
cout << "今年 " << m_age << endl;
}
private:
//成员变量
string m_name;
int m_age;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Student s1 ("大黄蜂", 233);
s1.who();
// s1.m_name = "ayumi";
// s1.m_age = 33;
// cout << s1.m_name << endl;
// cout << s1.m_age << endl;
s1.setName("救护车");
s1.setAge(33);
s1.who();
s1.setName("威震天");
s1.setAge(-99);
s1.who();
s1.setName("擎天柱");
s1.setAge(666);
s1.who();
Student* s2 = new Student("铁皮", 333);
s2->who();
delete s2;s2 = NULL;
Student sa[3] = {
Student("大黄蜂", 233),
Student("擎天柱", 233),
Student("铁皮", 233)
};
sa[0].who();
sa[1].who();
sa[2].who();
Student* ps = new Student[3]{
Student("大黄蜂", 888),
Student("擎天柱", 999),
Student("铁皮", 77)
};
ps[0].who();
ps[1].who();
ps[2].who();
delete[] ps;
ps = NULL;
return a.exec();
}
qt开发环境 - c++类
于 2017-08-17 21:53:12 首次发布