1.打印车的信息
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car(){}
~Car(){}
void setInfo(string b,string c,int p)
{
brand=b;
color=c;
price=p;
}
string getBrand()
{
return brand;
}
string getColor()
{
return color;
}
int getPrice()
{
return price;
}
void showInfo()
{
// cout << "This is a " << color << " " << brand << " car costs " ;
cout << "This is a " << color << " " << brand << " car costs "<<price << " CNY." << endl;
// cout << "This is a " << getColor() << " " << getBrand() << " car that costs " << getPrice() << " CNY." << endl;
}
private:
string brand;
string color;
int price;
};
int main()
{
string brand, color;
int price;
cin >> brand >> color >> price;
Car car;
car.setInfo(brand, color, price);
car.showInfo();
return 0;
}
2.打印一维二维三维坐标
#include <iostream>
#include <string>
using namespace std;
class Point
{
public:
virtual void show(){
}
};
class Point1D : public Point
{
public:
int a;
Point1D(){}
~Point1D(){}
void set(int a)
{
this->a =a;
}
void show()
{
cout << "This is a one-dimensional point (" << a << ")." << endl;
}
};
class Point2D : public Point
{
public:
int a;
int b;
Point2D(){}
~Point2D(){}
void set(int a,int b)
{
this->a=a;
this->b=b;
}
void show()
{
cout << "This is a two-dimensional point ("<< a << "," << b << ")." << endl;
}
};
class Point3D : public Point
{
public:
int a;
int b;
int c;
Point3D(){}
~Point3D(){}
void set(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
}
void show()
{
cout << "This is a three-dimensional point (" << a << "," << b << "," << c << ")." << endl;
}
};
void ShowPoint(Point *p)
{
p->show();
}
int main()
{
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
Point1D p1;
Point2D p2;
Point3D p3;
p1.set(a);
p2.set(b, c);
p3.set(d, e, f);
ShowPoint(&p1);
ShowPoint(&p2);
ShowPoint(&p3);
return 0;
}
3.book.txt文件(待修改)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int TotalPriceInFile(char * file)
{
fstream is;
is.open(file,ios::in);
string s;
int n;
int m;
int total = 0;
while(!is.eof())
{
is>>s>>n>>m;
total += (n*m);
}
return total;
}
int main()
{
char fileName[200] = "books.txt";
int total = TotalPriceInFile(fileName);
cout << "The total price is " << total << "." << endl;
return 0;
}
4.交换数字数组
#include <iostream>
using namespace std;
class Array
{
friend ostream& operator<< (ostream & out,const Array & a)
{
for(int i=0;i<a.m;i++)
{
out << a.data[i] << " ";
}
return out;
}
friend istream& operator>> (istream & in,const Array & a)
{
for(int i=0;i<a.m;i++)
{
in >>a.data[i];
}
return in;
}
public:
int m;
double * data;
Array()
{
}
~Array()
{
delete [] data;
}
void setLength(int l)
{
m=l;
data=new double [m];
}
void swap(Array& a)
{
Array a1;
a1.setLength(m);
for(int i=0;i<a1.m;i++)
{
a1.data[i]=data[i];
}
//delete [] data;
this->setLength(a.m);
for(int i=0;i<a.m;i++)
{
this->data[i]=a.data[i];
}
//delete [] a.data;
a.setLength(a1.m);
for(int i=0;i<a1.m;i++)
{
a.data[i]=a1.data[i];
}
}
};
int main()
{
int m, n;
cin >> m >> n;
Array a1, a2;
a1.setLength(m);
a2.setLength(n);
cin >> a1;
cin >> a2;
a1.swap(a2);
cout << a1 << endl;
cout << a2 << endl;
return 0;
}