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;
    }

  • 相关阅读:
    [转]Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
    [转]OAuth 2.0
    SpringMVC之七:SpringMVC中使用Interceptor拦截器
    多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)
    读写分离
    SVN中检出(check out) 和 导出(export) 的区别
    Hbase之三:Hbase Shell使用入门
    hadoop之一:概念和整体架构
    Twitter Storm如何保证消息不丢失
    Twitter Storm: storm的一些常见模式
  • 原文地址:https://www.cnblogs.com/huninglei/p/5435869.html
Copyright © 2011-2022 走看看