简单类的定义和继承

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

转载于:https://www.cnblogs.com/huninglei/p/5435869.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/418603.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1898)vue之电商管理系统电商系统之渲染用户的对话框

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

Module build failed (from ./node_modules/sass-loader/lib/loader.js):

https://blog.csdn.net/man8023man/article/details/95590474

Intellij IDEA运行报Command line is too long解法

https://blog.csdn.net/wochunyang/article/details/84776813

hadoop-2.7.2 分布式集群搭建

1.机器信息 五台centos 64位机器 2.集群规划 Server Name Hadoop Cluster Zookeeper Ensemble HBase Cluster Hadoop01 Name node & Resource manager Master Hadoop02 Secondary name node Hadoop03 Data node & Node manager √ Region ser…

前端学习(1899)vue之电商管理系统电商系统之渲染添加用户的表单

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

前端学习(1900)vue之电商管理系统电商系统之渲染添加用户的表单自定义邮箱的规则

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

@Select注解的使用

https://blog.csdn.net/chengbin918/article/details/88571036

树状数组成段更新模板

这个成段的编写复杂度很低&#xff0c;不需要加大空间复杂度&#xff0c;便于处理成段加&#xff0c;询问每个位置的值的操作&#xff1a; #include <bits/stdc.h> using namespace std ; typedef long long ll ; typedef long double ld ; typedef unsigned long long u…

前端学习(1901)vue之电商管理系统电商系统之渲实现添加表单的重置规则

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

在循环里创建数据库连接,严重影响数据库性能

在循环里面去操作数据库肯定会慢的&#xff0c;而且还容易碰到新手&#xff0c;在循环里面创建数据库连接&#xff0c;会导致连接数满。一般查询的话&#xff0c;在外层一次性查询出来 然后 在逻辑层去循环组织数据。如果是新增&#xff0c;则可以考虑使用批量插入的方法去处理…

内存溢出

java.lang.OutOfMemoryError: Java heap space 原因&#xff1a;Heap内存溢出&#xff0c;意味着Young和Old generation的内存不够。 解决&#xff1a;调整java启动参数-Xms -Xmx 来增加Heap内存。 java.lang.OutOfMemoryError: unable to create new native thread 原因&#…

前端学习(1902)vue之电商管理系统电商系统之渲实现添加用户前的预先校验

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

git生成公钥和私钥

转自&#xff1a;http://blog.csdn.net/wqjsir/article/details/17386087/ 一、 Git windows 客服端&#xff08;MsysGit&#xff09;下载 下载地址&#xff1a;http://code.google.com/p/msysgit/ 二、从开始菜单中找到Git 点击Git Bash 弹出命令行窗体&#xff0c;如下图&…

后台导出大量数据超时报 nginx404错误

https://blog.csdn.net/weixin_30695195/article/details/95163683

前端学习(1903)vue之电商管理系统电商系统之调用api添加用户

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

每日站立会议08

会议图片&#xff1a; 会议内容&#xff1a;祖浩然&#xff1a;昨天&#xff1a;对数据库的操作有了大概的了解&#xff1b;今天&#xff1a;制作增加、修改、删除、查询学生信息功能&#xff0c;以及退出主界面&#xff1b;遇到的问题&#xff1a;如何将网页地址以超链接形式…

java 8排序

https://www.cnblogs.com/invoker-/p/7709411.html

前端学习(1904)vue之电商管理系统电商系统之修改用户的操作

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

前端学习(1905)vue之电商管理系统电商系统之根据用户id查询对应的信息

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

SQL的主键和外键约束 小记

http://www.cnblogs.com/ywb-lv/archive/2012/03/12/2391860.html转载于:https://www.cnblogs.com/chengjun/p/5443002.html