#ifndef SHAPE_H
#define SHAPE_H
#include<iostream> //賦值函式
#include<fstream>
#include<string>
using namespace std;
class Shape{
int obj_id; //物件識別符號
string des; //描述物件特征
public:
Shape(int a,string b):obj_id(a),des(b){} //建構式
Shape(Shape& p){obj_id=p.obj_id;des=p.des;} //拷貝建構式
Shape(){} //建構式
Shape& operator=(Shape& p){obj_id=p.obj_id;des=p.des;return *this;} //賦值函式
void Setdes(string a){des=a;}
string Getdes(){return des;}
int Getid(){return obj_id;}
virtual float Area()=0; //純虛函式
virtual void WriteToFile(ofstream& o)=0;//純虛函式
};
#endif
#include"Shape.h"
class Point:public Shape{//派生類
float x, y;
static int count;
public:
Point(int a,float b,float c,string d):x(b),y(c),Shape(a,d){count++;} //建構式
Point(Point& p):x(p.x),y(p.y),Shape(p){count++;} // //拷貝建構式
Point(){count++;} ///建構式
Point& operator=(Point& p); //
virtual float Area(){return 0;}
virtual void WriteToFile(ofstream& o); //需要實作
static int Getcount(){return count;}
float Getx(){return x;}
float Gety(){return y;}
friend ifstream& operator<<(ifstream& in,Point& p);
};
int Point::count=0;
ifstream& Point::operator<<(ifstream& in,Point& p)
{
int b;
string a;
cout<<p.x<<endl;
in>>b>>p.x>>p.y;
in>>a;
Shape::Setdes(a);
return in;
}
void Point::WriteToFile(ofstream& o)
{
o<<Getid()<<" ("<<x<<","<<y<<") "<<Getdes()<<endl;
}
ostream& operator<<(ostream& o,Point& p)
{
o<<p.Getid()<<" ("<<p.Getx()<<","<<p.Gety()<<") "<<p.Getdes()<<endl;
return o;
}
Point& Point::operator=(Point& p)//
{
x=p.Getx();
y=p.Gety();
Shape::operator=(p);
return *this;
}
int main()
{
ofstream fileout("point.txt",ios::out);
Point p1(1,2,1,"House");
Point p2(2,3,4,"Hospital");
Point p3(3,4,5,"Park");
Point p4(p3);
cout<<p1;
cout<<p2;
cout<<p4;
p4=p1;
cout<<p4;
p1.WriteToFile(fileout);
p2.WriteToFile(fileout);
p3.WriteToFile(fileout);
cout<<Point::Getcount()<<endl;;
fileout.close();
return 0;
}
怎么改啊 dev編譯不了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61518.html
標籤:基礎類
上一篇:c++
