C++第12週moocオンライン評価-第12週統一インタフェースの異なる実装-マルチステート性(虚関数とシンボルリロード)
8929 ワード
//1 (20 )
// :
// Pet, Speak, “How does a pet speak ? ”; Cat Dog, Speak :“miao!miao!” “wang!wang!”。 Pet,Cat Dog , Pet , Pet,Cat Dog , Speak , 。
//
// :
//
//
// :
// Speak
//
// :
//
//
// :
//How does a pet speak ?
//miao!miao!
//wang!wang!
#include
using namespace std;
class Pet
{
public:
Pet()
{}
~Pet()
{}
virtual void Speak()
{
cout << "How does a pet speak?" << endl;
}
private:
};
class Cat:public Pet
{
public:
Cat()
{}
~Cat()
{}
virtual void Speak()
{
cout << "miao!miao!" << endl;
}
private:
};
class Dog:public Pet
{
public:
Dog()
{}
~Dog()
{}
virtual void Speak()
{
cout << "wang!wang!" << endl;
}
private:
};
int main()
{
Pet pet1;
Dog dog1;
Cat cat1;
Pet *point;
point = &pet1;
point->Speak();
point = &cat1;
point->Speak();
point = &dog1;
point->Speak();
system("pause");
return 0;
}
//2 (20 )
// :
// Pet, : , ; : ; ; Speak GetInfo;
// Pet Cat Dog, Speak , GetInfo Cat Dog 。 Pet , Cat Dog , GetInfo Speak , 。
//
// :
//
//
// :
// , , .( , )
//
// :
//mikey 2 blue
//benben 1 black
//
// :
// : mikey
// : 2
// : blue
// : miao!miao!
// : benben
// : 1
// : black
// : wang!wang!
//
// :500ms :32000kb
#include
#include
using namespace std;
class Pet
{
protected:
char Name[10];
int Age;
char Color[10];
public:
Pet(char name[],int age,char color[])
{
this->Age = age;
strcpy(this->Color , color);
strcpy(this->Name, name);
}
~Pet()
{}
virtual void Speak() = 0;
virtual void GetInfo() = 0;
};
class Cat:public Pet
{
public:
Cat(char name[], int age, char color[]):
Pet(name,age,color)
{}
~Cat()
{}
virtual void Speak()
{
cout << " :miao!miao!"<> Name >> Age >> Color;
Cat cat1(Name, Age, Color);
cin >> Name >> Age >> Color;
Dog dog1(Name, Age, Color);
cat1.GetInfo();
cat1.Speak();
dog1.GetInfo();
dog1.Speak();
system("pause");
return 0;
}
//3 (20 )
// :
// , ( + ) ( = ) 。
//
// :
//
//
// :
// ,
//
// :
//1 2
//3 4
//
// :
//1 + j2
//3 + j4
//4 + j6
// :500ms :32000kb
#include
using namespace std;
class Complex
{
private:
double real;
double img;
public:
Complex(double real = 0,double img = 0)
{
this->real = real;
this->img = img;
}
Complex operator +(Complex &c)
{
Complex temp;
temp.real = real + c.real;
temp.img = img + c.img;
return temp;
}
//Complex operator =(Complex &d)
//{
// this->real = d.real;
// this->img = d.img;
// return *this;
//}
~Complex()
{
}
void Print()
{
cout << real;
if (img>=0)
{
cout << "+";
cout << "j" << img << endl;
}
else
{
cout << "-";
cout << "j" << -img << endl;
}
}
};
int main()
{
double real;
double img;
cin >> real >> img;
Complex a1(real, img);
cin >> real >> img;
Complex b1(real, img);
a1.Print();
b1.Print();
Complex c1;
c1 = (a1 + b1);
c1.Print();
system("pause");
return 0;
}
//4 (20 )
// :
// , 。 A,B,C m ,n , C = A + B 。
//
// :
// ,
//
// :
// 。 , , 。
//
// :
//2 3
//1 3 2
//4 2 5
//2 3 4
//3 2 6
//
// :
//3 6 6
//7 4 11
//
// : , 。
// :500ms :32000kb
#include
using namespace std;
class Mat
{
private:
int row, col;
double *data;
public:
Mat(int row, int col)
{
this->row = row;
this->col = col;
this->data = new double[row*col];
}
void dataLoad()
{
for (int i = 0; i < row*col; i++)
{
cin >> data[i];
}
}
Mat operator+(Mat &mat)
{
static Mat temp(row,col);
for (int i = 0; i < row*col; i++)
{
temp.data[i] = this->data[i] + mat.data[i];
}
return temp;
}
void dataPrint()
{
int index = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << this->data[index];
if ( j != col-1)
{
cout << " ";
}
else
{
cout << endl;
}
index++;
}
}
}
~Mat()
{
}
};
int main()
{
int row, col;
cin >> row>>col;
Mat mat1(row, col);
mat1.dataLoad();
Mat mat2(row, col);
mat2.dataLoad();
Mat mat3(row,col);
mat3 = (mat1 + mat2);
mat3.dataPrint();
system("pause");
return 0;
}
//5 (20 )
// :
// Shape,
// printName() 、 printArea() 。 Shape 5 :Circle( ), 、Square( )
//, 、Rectangle( ) , 、Trapezoid( ) , 、 、Triangle( )
//, 。 , , , , 。
//
// :
// 、 、 、 、
//
// :
// , 。 3.14159
// 、 。
// :
//10
//5
//2 4
//1 2 3
//4 3
//
// :
// : = 10, : 314.159
// : = 5, : 25
// : = 2, = 4, : 8
// : = 1, = 2, = 3, : 4.5
// : = 4, = 3, : 6
// : 357.659
//
// :500ms :32000kb
#include
# define PI 3.14159
using namespace std;
class Shape
{
private:
public:
virtual void printName() = 0;
virtual double printArea() = 0;
Shape()
{}
~Shape()
{}
};
class Circle:public Shape
{
private:
double radius;
public:
Circle(double radius)
{
this->radius = radius;
}
virtual void printName()
{
cout << " : =" << radius << ",";
}
virtual double printArea()
{
double area;
area = radius*radius*PI;
cout << " :" << area<width = width;
this->height = height;
}
virtual void printName()
{
cout << " : =" << height << ", ="<side = side;
}
virtual void printName()
{
cout << " : =" << side << ",";
}
virtual double printArea()
{
double area;
area = side*side;
cout << " :" << area << endl;
return area;
}
~Square()
{
}
};
class Trapezoid :public Shape
{
private:
double upside;
double downside;
double height;
public:
Trapezoid(double upside,double downside,double height)
{
this->upside = upside;
this->downside = downside;
this->height = height;
}
virtual void printName()
{
cout << " : =" << upside << ", ="<downside = downside;
this->height = height;
}
virtual void printName()
{
cout << " : =" << downside << ", =" << height << ",";
}
virtual double printArea()
{
double area;
area = downside / 2 * height;
cout << " :" << area << endl;
return area;
}
~Triangle()
{
}
};
int main()
{
double sumArea = 0;
double radius, side, heightRect, widthRect, upsideTra, downsideTra, heightTra, heightTri, downsideTri;
cin >> radius >> side >> heightRect >> widthRect >> upsideTra >> downsideTra >> heightTra >> downsideTri >> heightTri;
Shape *all[5];
all[0] = new Circle(radius);
all[1] = new Square(side);
all[2] = new Rectangle(widthRect, heightRect);
all[3] = new Trapezoid(upsideTra, downsideTra, heightTra);
all[4] = new Triangle(downsideTri, heightTri);
for (int i = 0; i < 5; i++)
{
all[i]->printName();
sumArea = sumArea + all[i]->printArea();
}
cout << " :" << sumArea << endl;
system("pause");
return 0;
}