[zhujie]第九周C++实验

//7-5
#include <iostream>
#include <string>
using namespace std;

const double PI = 3.14;

class Shape{
public:
    Shape(){}
    ~Shape(){}
    virtual double getArea(){return -1;}//重写虚拟函数来实现对基类虚拟函数的覆盖
};

class Circle:public Shape{
public:
    Circle(double radius):radius(radius){}
    double getArea(){return PI*radius*radius;}
    ~Circle(){}
private:
    double radius;
};

class Rectangle:public Shape{
public:
    Rectangle(double width,double height):width(width),height(height){}
    double getArea(){return width*height;}
    ~Rectangle(){}
private:
    double width,height;
};

class Square:public Rectangle{
public:
    Square(double length):Rectangle(length,length){}
    ~Square(){}
};

int main(){
    Shape *sp;
    sp = new Circle(5.0);
    cout<< "Area of the Circle : " << sp->getArea()<<endl;
    delete sp;
    sp = new Rectangle(10.0,4.0);
    cout<< "Area of the Rectangle : " << sp->getArea()<<endl;
    delete sp;
    sp = new Square(4.0);
    cout<< "Area of the Square : " << sp->getArea()<<endl;
    delete sp;
    return 0;
}
//7-11
#include <iostream>
using namespace std;

class BaseClass{
public:
    void fn1(){
        cout<< "fn1() of BaseClass " <<endl;
    }
    void fn2(){
        cout<< "fn2() of BaseClass " <<endl;
    }
};

class DerivedClass:public BaseClass{
public:
    void fn1(){
        cout<< "fn1() of DerivedClass " <<endl;
    }
    void fn2(){
        cout<< "fn2() of DerivedClass " <<endl;
    }
};

int main(){
    DerivedClass aDerivedClass;
    DerivedClass *pDerivedClass = &aDerivedClass;
    BaseClass *pBaseClass = &aDerivedClass;
    //三种调用
    aDerivedClass.fn1();
    aDerivedClass.fn2();
    pBaseClass->fn1();
    pBaseClass->fn2();
    pDerivedClass->fn1();
    pDerivedClass->fn2();
    return 0;
}  
{//7-8 魔改版
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int MAXSIZE = 105;
struct Page{
    int pagesize;
    int linenum;
    string text;
};
int getsize(const std::string& s, const std::string& c)
{
    vector<string> v;
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2-pos1));

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length())
        v.push_back(s.substr(pos1));
    return v.size();
}
class Document{
public:
    Document(){}
    Document(string name):name(name){}
    Document(string name,string text):name(name),text(text){}
    void initDocument(string name){
        this->name = name;
    }
    /*
    void showName(){
        cout<< name <<endl;
    }
    void showText(){
        cout<< text <<endl;
    }
    */
    string getName(){
        return name;
    }
    void textAppend(string addString){
        text.append(addString);
    }
    string getText(){
        return text;
    }
private:
    string name;
    string text;
};

class Book:public Document{
public:
    Book(){}
    Book(string name):Document(name){}
    void initBook(string name,int pagecount);

    int getPagecount(){
        return pagecount;
    }
    void setText(int aPage,string aString){
        page[aPage].text.append(aString);
    }
    Page getPage(int aPage){
        return page[aPage];
    }
    ~Book(){}
private:
    string bookname;
    Page page[MAXSIZE];
    int pagecount;

};
void Book::initBook(string name,int pagecount){
    initDocument(name);
    this->bookname = name;
    this->pagecount = pagecount;
    for(int i = 1;i <= pagecount;i ++){
        string s;
        int cnt = 0;
        page[i].pagesize = 0;
        while(getline(cin,s) && s.find("#")==string::npos){
            cnt ++;
            page[i].pagesize += getsize(s," ");
            //page[i].pagesize += s.size();
            page[i].text.append(s + "\n");

        }
        page[i].linenum = cnt;
    }
}
int main(){
    string tmp;
    Document doc("C++ Book","PRINTF HELLO WORLD!");
    cout<< "DOC NAME : " << doc.getName() <<endl;
    cout<< "DOC TEXT :" << doc.getText() <<endl;
    doc.textAppend("\nI LOVE PROGRAMING!");
    cout<< "TEXT AFTER APPEND:" << doc.getText() <<endl;
//测试Book类
    int n;
    string str;
    cin>> str >> n;
    Book abook;
    abook.initBook(str,n);
    cout<< "Book name: " << abook.getName() <<endl;
    cout<< "Book pagecount:" << abook.getPagecount() <<endl;
    int sizep = abook.getPagecount();
    for(int i=1;i<=sizep;i++){
        Page p = abook.getPage(i);
        cout<< "Page " << i <<endl;
        cout<< "Line Number:" << p.linenum <<endl;
        cout<< "Pagesize:" << p.pagesize <<endl;
        cout<< "Text: \n" << p.text <<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值