⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 资深C++讲师授课代码
💻 TXT
📖 第 1 页 / 共 4 页
字号:
bash-2.05$ cat memberptr.cc
#include <iostream>
using namespace std;

struct Date{
        int year;
        int month;
        int day;
};

int main()
{
        Date d1={2008,8,15}, d2={2020,2,20};
        int Date::*p = NULL;
        p = &Date::month;
        cout << d1.*p << ',' << d2.*p << endl;
        p = &Date::year;
        cout << d1.*p << ',' << d2.*p << endl;
        Date* p1=&d1,* p2=&d2;
        p = &Date::day;
        cout << p1->*p << ',' << p2->*p << endl;
}


bash-2.05$ cat returnref.cc
#include <iostream>
using namespace std;

int& max(int& a, int& b)
{
        if(a>b)
                return a;
        else
                return b;
}
int main()
{
        int m=10, n=20;
        max(m, n) = 6;
        cout << "m=" << m << ", n=" << n << endl;
        max(m, n) = 5;
        cout << "m=" << m << ", n=" << n << endl;
}



bash-2.05$ cat class.cc
#include <iostream>
using namespace std;

class Date{
        int year;
        int month;
        int day;
public:
        void set(){
                year = 2008;
                month = 8;
                day = 15;
        }
        void print(){
                cout << year << "年" << month << "月" << day << "日" << endl;
        }
        bool leap(){
                if(year%4==0&&year%100!=0||year%400==0)
                        return true;
                return false;
        }
        void set(int y, int m, int d){
                year = y;
                month = m;
                day = d;
        }
};
int main()
{
        Date d;
        d.set();
        d.print();
        cout << (d.leap()?"run年":"平年") << endl;
        d.set(2002, 9, 1);
//      d.month = 5;//cannot access the member 'month'
        d.print();
        cout << (d.leap()?"run年":"平年") << endl;
}


bash-2.05$ cat funcptr2.cc
#include <iostream>
using namespace std;

struct Person{
        char name[20];
        bool gender;
        int age;
        double income;
};
bool isFemale(Person p)
{
        if(p.gender==false)
                return true;
        else
                return false;
}
bool age20(Person p)
{
        if(p.age>=20)
                return true;
        else
                return false;
}
bool lowIncome(Person p)
{
        if(p.income<1000)
                return true;
        else
                return false;
}
void select(Person s[], int n, bool(*f)(Person))
{
        for(int i=0; i<n; i++)
                if((*f)(s[i]))
                        cout << s[i].name << endl;
}
int main()
{
        Person s[3]={
                {"程一航", true, 21, 800},
                {"李燕成", true, 20, 1000},
                {"郭英", false, 18, 900}
        };
        cout << "选美女!" << endl;
        select(s, 3, &isFemale);
        cout << "选年龄够20的人!" << endl;
        select(s, 3, &age20);
        cout << "选收入低于1000的人!" << endl;
        select(s, 3, &lowIncome);
}


bash-2.05$ cat newdelete.cc
#include <iostream>
using namespace std;

int main()
{
        double* p1 = NULL;
        p1 = new double;
        cout << "p1=" << p1 << endl;
        cout << "*p1=" << *p1 << endl;
        double* p2 = NULL;
        p2 = new double(9.9);
        cout << "p2=" << p2 << endl;
        cout << "*p2=" << *p2 << endl;
        delete p1; p1=NULL;
        delete p2; p2=NULL;
        double* p3 = NULL;
        p3 = new double(123.45);
        cout << "p3=" << p3 << endl;
        cout << "*p3=" << *p3 << endl;
        delete p3; p3=NULL;
}

bash-2.05$ cat struct.cc
#include <iostream>
using namespace std;
#include <cstring>

struct Student{
        char name[20];
        bool gender;
        char* addr;
};
void show(Student s)
{
        cout << (s.gender?"帅哥":"美女") << s.name << "住在" << s.addr << endl;
}
int main()
{
        Student gy;
        Student lhw;
        strcpy(gy.name,"郭英");
        gy.gender = false;
        gy.addr = new char[30];
        strcpy(gy.addr, "内蒙古工业大学3#215");
        lhw = gy;
        strcpy(lhw.name, "李红卫");
        lhw.gender = true;
        lhw.addr = new char[40];
        strcpy(lhw.addr, "爱民街爱民小区5栋123");
        show(gy);
        show(lhw);
        delete[] gy.addr; gy.addr=NULL;
        delete[] lhw.addr; lhw.addr=NULL;
}


cpp08


bash-2.05$ cat czq.cc
 #include "czq.h"
 #include <iostream>
 using namespace std;
 void Czq::work(){
     cout << "好好工作, 天天加班!" << endl;
 }

 class Aoiqowhfuqwjhfajhsfiu{
 public:
     Aoiqowhfuqwjhfajhsfiu(){
         cout << "这个语句会在main之前执行" << endl;
     }
     ~Aoiqowhfuqwjhfajhsfiu(){
         cout << "这个语句会在main之后执行" << endl;
     }
 };
 Aoiqowhfuqwjhfajhsfiu akjsdhfkjwhfiuqwhgfoeiqwhfoiuqgwo;


bash-2.05$ cat destructor.cc
#include <iostream>
using namespace std;
#include <cstring>

class Person{
        char name[20];
        bool gender;
public:
        Person(char *n, bool g):gender(g){
                strcpy(name, n);
                cout << (gender?"小男孩":"小女孩") << name << "出生了" << endl;
        }
        ~Person(){
                cout << name << (gender?"老大爷":"老太太") << "与世长辞了,让我
们共同哀悼!" << endl;
        }
};
int main()
{
        Person p1("张三", true);
        Person p2("李四", false);
        Person a[4]={
                Person("无名1", true),
                Person("无名2", true),
                Person("无名3", false),
                Person("无名4", true),
        };
        Person* p=new Person("王五", false);
        delete p; p=NULL;
        cout << "...百年之后..." << endl;
}


bash-2.05$ cat string.cc
#include <iostream>
using namespace std;
#include <cstring>
class String{
    int len;
    char* p;
public:
    String();//申请一块100个字节的空间,放上一个空字符串""
    String(const char* s);//申请一块空间把传来的字符串保存一份
    void set(const char* s);//把传来的字符串复制一份到p指的空间中
                            //如果p指的空间不够大,就重新申请一块
    void print();//用来显示p指的字符串
    ~String();//释放p指的堆空间
};
String::String():len(100){
        p = new char[100];
        p[0] = '\0';
}
String::String(const char* s):len(strlen(s)+1){
        p = new char[len];
        strcpy(p, s);
}
void String::set(const char* s){
        if(len<strlen(s)+1){
                delete[] p;
                len = strlen(s)+1;
                p = new char[len];
        }
        strcpy(p, s);
}
void String::print(){
        cout << p << endl;
}
String::~String(){
        delete[] p;
}

int main()
{
        String s1, s2("good");
        s1.print();
        s2.print();
        s1.set("hello everyone!");
        s2.set("nice to meet you all!");
        s1.print();
        s2.print();
}


bash-2.05$ cat array.cc
#include <iostream>
using namespace std;

class Array{
        int len;
        int* p;
public:
        Array(int n):len(n){
                cout << "create object at " << this << endl;
                p = new int[n];
                cout << "allocate heap memory at " << p << endl;
        }
        ~Array(){
                cout << "release heap memory at " << p << endl;
                delete[] p;
                cout << "destory object at " << this << endl;
        }
        int size(){
                return len;
        }
        void print(){
                for(int i=0; i<len; i++)
                        cout << p[i] << ' ';
                cout << endl;
        }
        void input(){
                cout << "input " << len << " integers:" << endl;
                for(int i=0; i<len; i++)
                        cin >> p[i];
        }
};
int main()
{
        Array a(10);
        a.input();
        a.print();
}



bash-2.05$ cat czq.h
#ifndef czq_h
#define czq_h 1

class Czq{
public:
        void work();
};

#endif



bash-2.05$ cat main.cc
#include <iostream>
using namespace std;
#include "czq.h"

int main()
{
        cout << "----begin main----" << endl;
        Czq c;
        c.work();
        cout << "----end main----" << endl;
}


bash-2.05$ cat this.cc
#include <iostream>
using namespace std;

class Time{
        int hour;
        int minute;
        int second;
public:
        void set(){
                cout << "input time(hh mm ss):";
                cin >> hour >> minute >> second;
        }
        void set(int hour, int minute, int second)
        {//this is a pointer to the object itself
                (*this).hour = hour;
                this->minute = minute;
                Time::second = second;
        }
        void print(){
                cout << hour << ':' << minute << ':' << second << endl;
        }
        void equal(const Time& t){
                if(hour==t.hour&&minute==t.minute&&second==t.second)
                        cout << "same time!" << endl;
                else
                        cout << "not same!" << endl;
        }
};

int main()
{
        Time t1, t2;
        t1.set();
        t2.set();
        t1.print();
        t2.print();
        t1.equal(t2);
        t1.set(10,0,5);
        t1.print();
}



bash-2.05$ cat constructor.cc
#include <iostream>
using namespace std;

class Time{
        int hour;
        int minute;
        int second;
public:
        Time();
        Time(int hour, int minute, int second)
                :hour(hour), minute(minute), second(second)
        {
                cout << "initialize..." << this << endl;
        }
        void print();
};
Time::Time()
        :hour(0),minute(0),second(0)
{
        cout << "initialize " << this << endl;
}
void Time::print()
{
        cout << hour << ':' << minute << ':' << second << endl;
}
int main()
{
        Time t1;
        Time t2;
        cout << "&t1=" << &t1 << endl;
        cout << "&t2=" << &t2 << endl;
        t1.print();
        Time t3(10,1,10);
        t3.print();
}


bash-2.05$ cat defaultcon.cc
#include <iostream>
using namespace std;

class A{
public:
};
class B{
public:
        B(int n){}
};
int main()
{
        A obj;
        //B obb;//ERROR!!
        B oba(10);
}


bash-2.05$ cat noname.cc
#include <iostream>
using namespace std;

class A{
public:
        A(){cout<<"A()"<<this<<endl;}
        ~A(){cout<<"~A()"<<this<<endl;}
        A(const A& o){cout<<"A(A)"<<this<<endl;}
};
void func1(A a)
{
}
A func2(A a)
{
        return a;
}
int main()
{
        A();
        A obj;
        A copy=obj;//A copy(obj);
        cout << "================" << endl;
        cout << "&obj=" << &obj << endl;
        cout << "&copy=" << &copy << endl;
        cout << "-----------------" << endl;
        func1(obj);
        cout << "-----------------" << endl;
        func2(obj);
        cout << "-----------------" << endl;
}












































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -