#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;
}
#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;
}
{
#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;
}
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].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;
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;
}