第一篇:
代码:
#include<iostream>
using namespace std;
//shape.h
class Shape
{
public:
virtual double perimeter()const(return 0;)
virtual double area()const{return 0;}
virtual const char * name()const {return"抽象图形";}
};
class Point
{
double x;
double y;
public:
Point(double x0,double y0):x(x0),y(y0){}
double getX()const {return x;}
double getY()const {return y;}
};
class Triangle:public Shape
{
Point point,point2,point3;
public:
Triangle(Point p1,Point p2,Point p3):point1(p1),point2(p2),point3(p3){}
double perimeter()const;
double area()const;
const char * name()const{return "三角形";}
};
//shape.cpp
#include "shape.h"
#include<cmath>
double length (Point p1,Point p2)
{
return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
}
double Triangle::perimeter()const
{
return length(point1,point2)+length(point1,point3)+length(point2,point3)
}
double Triangle::area()const
{
double s= perimeter()/2.0;
return sqrt(s*(s-length(point1,point2))*(s- length (point2,point3))*(s- length(point3,point1)));
}
//proj2.cpp
#include"shape.h"
#include<iostream>
using namespace std;
void show(shape&shape)
{
count<<"此图形是一个" <<shape.name()<<",周长 ="<< shape.perimeter()<<",面积 = "<<shape.area() <<endl;
}
int main()
{
Shape s;
Triangle tri(Point(0,2),Point(2,0),Point(0,0));
show(s);
show(tri);
return 0;
}
命令:
![](https://img-blog.csdnimg.cn/d6d14a007cb243d9a8cd7d5a97ca8531.png)
第二篇:
代码:
//proje3.h
#include<iostream>
#include<string>
using namespace std;
class Person
{
char name[20];
int age;
char * address;
public:
Person(){age = 0;address =0;}
void name_change(char *_name);
void age_change(int_age);
void address_chang(char*_add);
void info_display();
Person();
};
void writeToFile(const char*path);
proj3.cpp
#include<iostream>
#include<string>
#include"proj3.h"
using namespace std;
void Person::name_change(char*_name)
{
strcpy(name,_name);
}
void Person::address_change(int _age)
{
age =_age;
}
void Person::address_change(char* _add)
{
if(address!=NULL)operator delete[]address;
address= new char[strlen(_add)+1];
strcpy(address,_add);
}
void Person::info_display()
{
cout << nam