zoukankan      html  css  js  c++  java
  • 简单类的定义和继承

    class book
    {
        char* title;//书名
        int num_pages;//页数
        char * writer;//作者姓名
    public:
        book( char* the_title, int pages, const char* the_writer) :num_pages(pages)
        {
            title = new char[strlen(the_title) + 1];
            strcpy(title, the_title);
            writer = new char[strlen(the_writer) + 1];
            strcpy(writer, the_writer);
        }
        ~book()
        {
            delete[] title;
            delete[] writer;
        }
        int numOfPages()const
        {
            return num_pages;
        }
        /*一、概念

        当const在函数名前面的时候修饰的是函数返回值,在函数名后面表示是常成员函数,
        
          该函数不能修改对象内的任何成员,只能发生读操作,不能发生写操作。


            二、原理:

            我们都知道在调用成员函数的时候编译器会将对象自身的地址作为隐藏参数传递给函数,
            在const成员函数中,既不能改变this所指向的对象,也不能改变this所保存的地址,
            this的类型是一个指向const类型对象的const指针。*/
        const char* thetitle()const
        {
            return title;
        }
        const char* thewriter()
        {
            return writer;
        }
    };

    class teachingMaterial :public book
    {
        char * course;
    public:
        teachingMaterial( char* the_title, int pages, char* the_writer, const char* the_course)
            :book(the_title,pages,the_writer)
        {
            course = new char[strlen(the_course) + 1];
            strcpy(course, the_course);
        }
        ~teachingMaterial()
        {
            delete[] course;
        }
        const char* theCourse()const
        {
            return course;
        }
    };

    int _tmain()
    {
        teachingMaterial a_book("C++语言程序设计", 299, "张三", "面向对象的程序设计");
        cout << "教材名: " << a_book.thetitle() << endl;
        cout << "页数: " << a_book.numOfPages ()<< endl;
        cout << "作者: " << a_book.thewriter() << endl;
        cout << "相关课程: " << a_book.theCourse() << endl;
    }

  • 相关阅读:
    python-使用pyecharts绘制各省份985学校数量图
    python-将多个表格的信息合并到一个表格中
    python-使用百度AipOcr实现表格文字图片识别
    python安装OCR识别库
    python-一种去掉前后缀获取子串的方法
    python-一种字符串排序方式
    How to write educational schema.
    RabbitMq related
    OPENId是什么, OAUTH 是什么
    使用abp的 redis cache
  • 原文地址:https://www.cnblogs.com/huninglei/p/5435869.html
Copyright © 2011-2022 走看看