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

📄 新建 文本文档 (2).txt

📁 资深C++讲师授课代码
💻 TXT
字号:
cpp09

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

class Printer{
        int pages;
public:
        void setPages(int n);
        int getPages();
        virtual void print();
};
class DSPrinter : public Printer{
        bool doubleside;
public:
        DSPrinter();
        void setDoubleSide(bool ds);
        void print();
};
void Printer::setPages(int n)
{
        pages = n;
}
int Printer::getPages()
{
        return pages;
}
void Printer::print()
{
        for(int i=0; i<pages; i++)
                cout << "在第" << i+1 << "张纸上印第" << i+1 << "页" << endl;
}
DSPrinter::DSPrinter():doubleside(false)
{
}
void DSPrinter::setDoubleSide(bool ds)
{
        doubleside = ds;
}
void DSPrinter::print()
{
        if(!doubleside)
                Printer::print();
        else for(int i=1; i<=getPages(); i++)
                cout << "在第" << (i+1)/2 << "张纸" << (i%2==1?"正":"背") << "面

印第" << i << "页" << endl;
}
class Computer{
        Printer* p;
public:
        Computer():p(NULL){}
        void install(Printer* p){
                this->p = p;
        }
        void printDocument(){
                if(p==NULL){
                        cout << "没有安装列印机, 无法列印" << endl;
                        return;
                }
                int n;
                cout << "请输入页数:";
                cin >> n;
                p->setPages(n);
                p->print();
        }
};
int main()
{
        DSPrinter d;
        Computer c;
        c.printDocument();
        d.setDoubleSide(true);
        c.install(&d);
        c.printDocument();
        Printer prn;
        c.install(&prn);
        c.printDocument();
}



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

class Person{
protected:
        char name[20];
        bool gender;
        int age;
public:
        void eat(const char* food){
                cout << name << "在吃" << food << endl;
        }
        void sleep(){
                cout << name << "睡觉了" << endl;
        }
        Person(const char* name, bool gender){
                strcpy(this->name, name);
                this->gender = gender;
                age = 0;
        }
        Person(){
                strcpy(name, "无名");
                gender = true;
        }
        void show(){
                cout << "我是" << (gender?"帅哥":"美女") << name << ", 今年 " <<

 age << ", 希望跟大家交个朋友!" << endl;
        }
        void grow(int n){
                age += n;
        }
};
class Teacher : public Person{
        string course;
public:
        Teacher(const char* name , bool gender):Person(name,gender){
        }
        void setCourse(const string& course){
                this->course = course;
        }
        void teach(const string& students){
                cout << name << "老师在给" << students << "讲" << course << "课
程" << endl;
        }
        void show(bool b){
                cout << "我是一名";
                if(b)
                        cout << (gender?"男":"女");
                cout << course << "老师,我叫" << name << ", 希望咱们一起把这个
课程学好!" << endl;
        }
};
int main()
{
        Teacher t("陈宗权", true);
        t.setCourse("C++");
        Person p("芙蓉", false);
        p.grow(38);
        t.grow(35);
        p.show();
        t.show(true);
        t.Person::show();
}



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

class Person{
        char name[20];
        bool gender;
        int age;
public:
        void eat(const char* food){
                cout << name << "在吃" << food << endl;
        }
        void sleep(){
                cout << name << "睡觉了" << endl;
        }
        Person(const char* name, bool gender){
                strcpy(this->name, name);
                this->gender = gender;
                age = 0;
        }
        Person(){
                strcpy(name, "无名");
                gender = true;
        }
        void show(){
                cout << "我是" << (gender?"帅哥":"美女") << name << ", 今年 " <<

 age << ", 希望跟大家交个朋友!" << endl;
        }
        void grow(int n){
                age += n;
        }
        const char* getName(){return name;}
        bool getGender(){return gender;}
};
class Teacher : public Person{
        string course;
public:
        Teacher(const char* name , bool gender):Person(name,gender){
        }
        void setCourse(const string& course){
                this->course = course;
        }
        void teach(const string& students){
                cout << getName() << "老师在给" << students << "讲" << course <<

 "课程" << endl;
        }
        void show(bool b){
                cout << "我是一名";
                if(b)
                        cout << (getGender()?"男":"女");
                cout << course << "老师,我叫" << name << ", 希望咱们一起把这个
课程学好!" << endl;
        }
};
int main()
{
        Teacher t("陈宗权", true);
        t.setCourse("C++");
        Person p("芙蓉", false);
        p.grow(38);
        t.grow(35);
        p.show();
        t.show(true);
        t.Person::show();
}



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

class Printer{
        int pages;
public:
        void setPages(int n);
        int getPages();
        void print();
};
class DSPrinter : public Printer{
        bool doubleside;
public:
        DSPrinter();
        void setDoubleSide(bool ds);
        void print();
};
void Printer::setPages(int n)
{
        pages = n;
}
int Printer::getPages()
{
        return pages;
}
void Printer::print()
{
        for(int i=0; i<pages; i++)
                cout << "在第" << i+1 << "张纸上印第" << i+1 << "页" << endl;
}
DSPrinter::DSPrinter():doubleside(false)
{
}
void DSPrinter::setDoubleSide(bool ds)
{
        doubleside = ds;
}
void DSPrinter::print()
{
        if(!doubleside)
                Printer::print();
        else for(int i=1; i<=getPages(); i++)
                cout << "在第" << (i+1)/2 << "张纸" << (i%2==1?"正":"背") << "面

印第" << i << "页" << endl;
}
int main()
{
        DSPrinter d;
        d.setPages(9);
        d.print();
        d.setDoubleSide(true);
        d.print();
}



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

class USB {//abstract class
public:
        virtual const char* getType()=0;
        virtual void work()=0;
};
class Computer{
        USB* p;
public:
        Computer():p(NULL){}
        void plugin(USB* p){
                this->p = p;
                cout << "发现" << p->getType() << endl;
                cout << p->getType() << "已经安装并可以使用了" << endl;
        }
        void plugin(USB& u){
                cout << "发现" << u.getType() << endl;
                cout << "仅仅测试一下引用实现多态" << endl;
                p = &u;
        }
        void use(){
                p->work();
        }
};
class UDisk : public USB{
        const char* getType(){
                return "USB移动硬盘";
        }
        void work(){
                cout << "用USB移动硬盘存储数据" << endl;
        }
};
class UCamera : public USB{
        const char* getType(){
                return "USB摄像头";
        }
        void work(){
                cout << "用USB摄像头进行视频聊天" << endl;
        }
};
int main()
{
        Computer c;
        UCamera uc;
        UDisk ud;
        c.plugin(&uc);
        c.use();
        c.plugin(&ud);
        c.use();
        c.plugin(uc);
        c.plugin(ud);
}

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

class F{
        int n;
        int d;
public:
        F(int n=0, int d=1):n(n),d(d){reduce();}
private:
        void reduce(){
                for(int i=n; i>1; i--){
                        if(n%i==0&&d%i==0){
                                n/=i;
                                d/=i;
                                break;
                        }
                }
        }
public:
        void print()const{
                cout << n << "/" << d << endl;
                //++n;  //ERROR!!
        }
        void func(){
                cout << "func()" << endl;
        }
        void func()const{
                cout << "func()const" << endl;
        }
};
int main()
{
        F f1(8, 12);
        F f2(18, 6);
        f1.print();
        f2.print();
        const F pi(355, 113);
        pi.print();
        pi.func();
        f1.func();
}


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

class Person{
protected:
        char name[20];
        bool gender;
        int age;
public:
        void eat(const char* food){
                cout << name << "在吃" << food << endl;
        }
        void sleep(){
                cout << name << "睡觉了" << endl;
        }
        void birth(const char* name, bool gender){
                strcpy(this->name, name);
                this->gender = gender;
                age = 0;
        }
};
class Teacher : public Person{
        string course;
public:
        void setCourse(const string& course){
                this->course = course;
        }
        void teach(const string& students){
                cout << name << "老师在给" << students << "讲" << course << "课
程" << endl;
        }
};
int main()
{
        Teacher t;
        t.birth("陈宗权", true);
        t.setCourse("C++");
        t.eat("蒙餐");
        t.teach("内蒙古工业大学同学");
        t.sleep();
}


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

class Person{
protected:
        char name[20];
        bool gender;
        int age;
public:
        void eat(const char* food){
                cout << name << "在吃" << food << endl;
        }
        void sleep(){
                cout << name << "睡觉了" << endl;
        }
        void birth(const char* name, bool gender){
                strcpy(this->name, name);
                this->gender = gender;
                age = 0;
        }
        void show(){
                cout << "我是" << (gender?"帅哥":"美女") << name << ", 今年 " <<

 age << ", 希望跟大家交个朋友!" << endl;
        }
        void grow(int n){
                age += n;
        }
};
class Teacher : public Person{
        string course;
public:
        void setCourse(const string& course){
                this->course = course;
        }
        void teach(const string& students){
                cout << name << "老师在给" << students << "讲" << course << "课
程" << endl;
        }
        void show(bool b){
                cout << "我是一名";
                if(b)
                        cout << (gender?"男":"女");
                cout << course << "老师,希望咱们一起把这个课程学好!" << endl;
        }
};
int main()
{
        Teacher t;
        t.birth("陈宗权", true);
        t.setCourse("C++");
        Person p;
        p.birth("芙蓉", false);
        p.grow(38);
        t.grow(35);
        p.show();
        t.show(true);
        t.Person::show();
}





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

class USB {
public:
        virtual const char* getType(){return "usb设备";}
        virtual void work(){cout<<"usb设备开始工作"<<endl;}
};
class Computer{
        USB* p;
public:
        Computer():p(NULL){}
        void plugin(USB* p){
                this->p = p;
                cout << "发现" << p->getType() << endl;
                cout << p->getType() << "已经安装并可以使用了" << endl;
        }
        void use(){
                p->work();
        }
};
class UDisk : public USB{
        const char* getType(){
                return "USB移动硬盘";
        }
        void work(){
                cout << "用USB移动硬盘存储数据" << endl;
        }
};
class UCamera : public USB{
        const char* getType(){
                return "USB摄像头";
        }
        void work(){
                cout << "用USB摄像头进行视频聊天" << endl;
        }
};
int main()
{
        Computer c;
        UCamera uc;
        UDisk ud;
        c.plugin(&uc);
        c.use();
        c.plugin(&ud);
        c.use();
}


cpp10



⌨️ 快捷键说明

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