标签:ace 构造函数 false 默认 namespace round ring cst 返回
类的学习
#include<iostream> #include<string> using namespace std; class Cperson { public: string Name; int *p; //构造函数与类同名,可以有参数也可以没有参数 //无返回值 //在创建对象时一定会调用的函数 //类里面默认一个构造函数,无参数 //构造函数的作用是给成员变量赋值 Cperson() { cout << "Cperson()" << endl; p = new int[3]; } //析构函数,反向的构造函数 //在对象被回收时,一定会被调用的函数 //在一个类里只有一个 //省略了回收空间的操作,防止忘记 ~Cperson() { delete [ ] p; cout << "~Cperson()" << endl; } private: int m_BankPassword; public: int GetPassWord() { return m_BankPassword; } }; int main() { Cperson ps; ps.GetPassWord(); return 0; }
练习1
#include<iostream> using namespace std; class CStudent { public: int id; int m_Chinese; int m_Math; CStudent(int ID, int a, int b) { id = ID; m_Chinese = a; m_Math = b; } int Average() { return (m_Chinese + m_Math) / 2; } bool IsPass() { if (m_Chinese < 60 || m_Math < 60) { return false; } return true; } }; int main() { int id; int m_ch; int m_ma; cin >> id >> m_ch >> m_ma; CStudent st1(id,m_ch,m_ma); cout << st1.Average() << endl; if (st1.IsPass()) { cout << "pass" << endl; } else { cout << "not pass" << endl; } return 0; }
练习2
#include<iostream> using namespace std; class Timer { public: int h; int m; int s; void GetTime(int hour,int minute,int second) { h = hour; m = minute; s = second; } void ShowTime() { if (h < 24 || h >= 0 || m < 60 || m >= 0 || s < 60 || s >= 0) { cout << h << ":" << m << ":" << s << endl; } } }; int main() { Timer st; st.GetTime(23, 34, 56); st.ShowTime(); }
标签:ace 构造函数 false 默认 namespace round ring cst 返回
原文地址:https://www.cnblogs.com/NoisyHu/p/10314772.html