C++笔记

约分

#include
using namespace std;
class Fract
{
    int m_m;
    int m_z;
public:
     Fract(int z,int m)//没有返回值 (构造函数)
    {
        m_m=m;
        m_z=z;
    }
    Fract()//没有返回值 (构造函数)
    {
        m_m=100;
        m_z=10;
        cout<<"Fract construct"<0;i--)
        {
            if((m_m%i==0)&&(m_z%i==0))
            {
                m_m/=i;
                m_z/=i;
                break;
            }
        }
    }
};

int main()
{
    Fract f(10,20);
//  f.set(16,48);   
    f.print();
    Fract f1(30,5);
//  f1.set(16,48);  
    f1.print();
}
运行结果:
1/2
6/1

int main()
{
    
    //Fract f(10,20);
//  f.set(16,48);   
    //f.print();
    Fract f1[5];//构造类对象数组,只能调用无参数构造函数,调用了5次 

}
运行结果:
Fract construct
Fract construct
Fract construct
Fract construct
Fract construct

时钟

#include
#include
#include 
//#include
#include
using namespace std;

class Time
{
    int m_iHour;//小时 
    int m_iMinute;//分钟 
    int m_iSecond;//秒 
public:
    void set(int h,int m,int s)
    {
        m_iHour=h;
        m_iMinute=m;
        m_iSecond=s;
    }
    
    void show()
    {
        cout<

时钟2

#include
#include
using namespace std;
class Clock{
    public:
        Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){
        }
        void displayTime();
    private:
        short h;
        short m;
        short s;
};
void Clock::displayTime()
    {
        while(true)
        {
            cout<

析构函数

#include
using namespace std;
class A
{
    int m_a;
    public:
        A(int a)
        {
            m_a=a;
            cout<<"A construct"<

分配空间和释放空间

#include
#include
using namespace std;
class Test
{
    int *p;
    public:
        Test()
        {
            p=(int*)malloc(sizeof(int));
            *p=4;
            cout<<"Test construct"<

字符

#include
using namespace std;
int main()
{
    string s;
    // string s("weiwei");
    //string s1(5,'a');
    //string s(s1);
    s="hello shangqian";//赋值 
    s+="sq1409";//追加、连接 
    s="hello";
    //s.clear;                     //清空
    cout<<"size="<

点菜


#include
using namespace std;
class Waiter
{
    string m_name;//姓名 
    public:
        Waiter(string name):m_name(name)
        {           
        }
        ~Waiter()
        {
        }
    string getName()
    {
        return m_name;
    }
    void Serve(int iCount,string szFood);
    
};
void Waiter::Serve(int iCount,string szFood)
{
    cout<>szFood;
    cout<<"请输入菜的份数";
    cin>>iCount;
    cout<<"**************************"<>money;
    cout<<"**************************"<

运行的先后顺序

#include
using namespace std;
class Test
{
    int m_data;
    public:
        Test(int data):m_data(data)
        {
            cout<<"test("<

true or false(甩锅还是美女)

#include
using namespace std;
int main()
{
    bool bSet=true;
    cout<<"请输入你的性别:"<>bSet;
    cout<<"我叫Jack,是一位"<<(bSet?"甩锅":"美女")<

你可能感兴趣的:(C++笔记)