@Pinetrie
2019-06-21T00:23:54.000000Z
字数 6702
阅读 905
1、 录入下面程序,并分析结果:
#include <iostream>
#include <complex>
using namespace std;
class Base
{
public:
Base() {cout<<"Base-ctor"<<endl;}
~Base() {cout<<"Base-dtor"<<endl;}
virtual void f(int){cout<<"Base::f(int)"<<endl;}
virtual void f(double){cout<<"Base::f(double)"<<endl;}
virtual void g(int i=10){cout<<"Base::g()"<<i<<endl;}
};
class Derived : public Base
{
public:
Derived() {cout<<"Derived-ctor" <<endl;}
~Derived(){cout<<"Derived-dtor"<<endl;}
void f(complex<double>) {
cout<<"Derived::f(complex)"<<endl;
}
void g(int i=20){
cout<<"Derived::g()"<<i<<endl;
}
};
int main()
{
cout<<sizeof(Base)<<endl;
cout<<sizeof(Derived)<<endl;
Base b;
Derived d;
Base *pb=new Derived;
b.f(1.0);
d.f(1.0);
pb->f(1.0);
b.g();
d.g();
pb->g();
delete pb;
return 0;
}
2、 录入下面程序,并分析结果:
#include <iostream>
using namespace std;
class Base
{
public:
Base():data(count)
{
cout<<"Base-ctor"<<endl;
++count;
}
~Base()
{
cout<<"Base-dtor"<<endl;
--count;
}
static int count;
int data;
};
int Base::count;
class Derived : public Base
{
public:
Derived():data(count),data1(data)
{
cout<<"Derived-ctor"<<endl;
++count;
}
~Derived()
{
cout<<"Derived-dtor"<<endl;
--count;
}
static int count;
int data1;
int data;
};
int Derived::count=10;
int main()
{
cout<<sizeof(Base)<<endl;
cout<<sizeof(Derived)<<endl;
Base* pb = new Derived[3];
cout<<pb[2].data<<endl;
cout<<((static_cast<Derived*>(pb))+2)->data1<<endl;
delete[] pb;
cout<<Base::count<<endl;
cout<<Derived::count<<endl;
return 0;
}
3、 录入下面程序,修改错误。
#include <iostream>
#include <new>
#include <assert.h>
using namespace std;
class Abstract
{
public:
Abstract() {cout << "in Abstract()"<<endl;}
virtual void f() = 0;
};
class Subclass : public Abstract
{
public:
Subclass() {cout << "in Subclass()"<<endl;}
void f() {cout << "Subclass::f()"<<endl;}
};
int main()
{
Abstract *p = new Subclass;
p->f();
return 0;
4、 定义基类shape类为一个表示形状的抽象类
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14;
class shape
{
public:
virtual double area() = 0;
};
class triangle:public shape
{
public:
triangle(double a,double b,double c):a(a),b(b),c(c) {}
double area()
{
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
private:
double a,b,c;
};
class circles:public shape
{
public:
circles(double r):r(r) {}
double area()
{
return r * r * PI;
}
private:
double r;
};
int main()
{
shape *p;
triangle t(3,4,5);
circles c(10);
p=&t;
cout<<"triangle area:"<<p->area()<<endl;
p=&c;
cout<<"circles area:"<<p->area()<<endl;
return 0;
}
5、 定义一个抽象类Base和派生类Derived
#include <iostream>
using namespace std;
class Base
{
public:
virtual void abstractMethod() = 0;
};
class Derived:public Base
{
public:
void abstractMethod()
{
cout<<"Derived::abstractMethod is called"<<endl;
}
};
int main()
{
Base* pBase = new Derived;
pBase->abstractMethod();
delete pBase;
return 0;
}
1、 教材习题6_20:实现一个简单圆类
#include <iostream>
#include <cmath>
using namespace std;
const double Pi = 3.14;
class SimpleCircle
{
public:
void init(double r) {p = &r;}
void showarea() {cout<<(*p) * (*p) * Pi;}
void showr() {cout<<*p;}
private:
double *p = NULL;
};
int main()
{
SimpleCircle A;
A.init(10);
cout<<"Area:";
A.showarea();
//A.showr();//
return 0;
}
2、教材习题7_5:Shape类派生Rectangle和Circle类
#include <iostream>
using namespace std;
class Shape
{
public:
Shape(double a,double b):a(a),b(b) {}
Shape(double a):a(a) {}
double geta() {return a;}
double getb() {return b;}
private:
double a,b;
};
class Rectangle:public Shape
{
public:
Rectangle(double a,double b):Shape(a,b) {}
double getArea()
{
return geta() * getb();
}
};
class Circle:public Shape
{
public:
Circle(double a):Shape(a) {}
double getArea()
{
return geta() * geta() * 3.14;
}
};
int main()
{
double a,b,r;
cout<<"Input a,b:";
cin>>a>>b;
cout<<"Input r:";
cin>>r;
Rectangle X(a,b);
Circle Y(r);
cout<<"Rectangle Area:"<<X.getArea()<<",Circle Area:"<<Y.getArea()<<endl;
return 0;
}
3、 教材习题7_6:哺乳动物类Mammal派生出狗类Dog
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
Mammal(string name):name(name) {cout<<"Con.Mammal"<<endl;}
~ Mammal() {cout<<"Des.Mammal"<<endl;}
string getname() {return name;}
private:
string name;
};
class Dog:public Mammal
{
public:
Dog(string name):Mammal(name) {cout<<"Con.dog:"<<getname()<<endl;}
~ Dog() {cout<<"Des.dog:"<<getname()<<endl;}
};
int main()
{
cout<<"Input Dog Name:";
string name;
cin>>name;
Dog A(name);
return 0;
}
1、教材习题9_1:利用数组类模板Array解决学生成绩问题
#include <cassert>
#include <iostream>
using namespace std;
//数组类模板定义
template <class T>
class Array {
private:
T* list; //T类型指针,用于存放动态分配的数组内存首地址
int size; //数组大小(元素个数)
public:
Array(int sz = 50); //构造函数
Array(const Array<T> &a); //复制构造函数
~Array(); //析构函数
Array<T> & operator = (const Array<T> &rhs); //重载"="使数组对象可以整体赋值
T & operator [] (int i); //重载"[]",使Array对象可以起到C++普通数组的作用
const T & operator [] (int i) const; //"[]"运算符的const版本
operator T * (); //重载到T*类型的转换,使Array对象可以起到C++普通数组的作用
operator const T * () const; //到T*类型转换操作符的const版本
int getSize() const; //取数组的大小
void resize(int sz); //修改数组的大小
};
//构造函数
template <class T>
Array<T>::Array(int sz) {
assert(sz >= 0); //sz为数组大小(元素个数),应当非负
size = sz; // 将元素个数赋值给变量size
list = new T [size]; //动态分配size个T类型的元素空间
}
//析构函数
template <class T>
Array<T>::~Array() {
delete [] list;
}
//拷贝构造函数
template <class T>
Array<T>::Array(const Array<T> &a) {
//从对象x取得数组大小,并赋值给当前对象的成员
size = a.size;
//为对象申请内存并进行出错检查
list = new T[size]; // 动态分配n个T类型的元素空间
//从对象X复制数组元素到本对象
for (int i = 0; i < size; i++)
list[i] = a.list[i];
}
//重载"="运算符,将对象rhs赋值给本对象。实现对象之间的整体赋值
template <class T>
Array<T> &Array<T>::operator = (const Array<T>& rhs) {
if (&rhs != this) {
//如果本对象中数组大小与rhs不同,则删除数组原有内存,然后重新分配
if (size != rhs.size) {
delete [] list; //删除数组原有内存
size = rhs.size; //设置本对象的数组大小
list = new T[size]; //重新分配n个元素的内存
}
//从对象X复制数组元素到本对象
for (int i = 0; i < size; i++)
list[i] = rhs.list[i];
}
return *this; //返回当前对象的引用
}
//重载下标运算符,实现与普通数组一样通过下标访问元素,并且具有越界检查功能
template <class T>
T &Array<T>::operator[] (int n) {
assert(n >= 0 && n < size); //检查下标是否越界
return list[n]; //返回下标为n的数组元素
}
template <class T>
const T &Array<T>::operator[] (int n) const {
assert(n >= 0 && n < size); //检查下标是否越界
return list[n]; //返回下标为n的数组元素
}
//重载指针转换运算符,将Array类的对象名转换为T类型的指针,
//指向当前对象中的私有数组。
//因而可以象使用普通数组首地址一样使用Array类的对象名
template <class T>
Array<T>::operator T * () {
return list; //返回当前对象中私有数组的首地址
}
template <class T>
Array<T>::operator const T * () const {
return list; //返回当前对象中私有数组的首地址
}
//取当前数组的大小
template <class T>
int Array<T>::getSize() const {
return size;
}
// 将数组大小修改为sz
template <class T>
void Array<T>::resize(int sz) {
assert(sz >= 0); //检查sz是否非负
if (sz == size) //如果指定的大小与原有大小一样,什么也不做
return;
T* newList = new T [sz]; //申请新的数组内存
int n = (sz < size) ? sz : size; //将sz与size中较小的一个赋值给n
//将原有数组中前n个元素复制到新数组中
for (int i = 0; i < n; i++)
newList[i] = list[i];
delete[] list; //删除原数组
list = newList; // 使list指向新数组
size = sz; //更新size
}
int main()
{
int n;
cout<<"Input n:";
cin>>n;
Array<double> a(n);
cout<<"Input scores:";
for(int i = 0;i < n;i++) cin>>a[i];
double sum = 0;
for(int i = 0;i < n;i++) sum += a[i];
cout<<"Average:"<<sum / n<<endl;
return 0;
}
2、教材习题9_10:利用插入排序函数模板进行排序
#include <iostream>
using namespace std;
//用直接插入排序法对数组A中的元素进行升序排列
template <class T>
void insertionSort(T a[], int n) {
int i, j;
T temp;
//将下标为1~n-1的元素逐个插入到已排序序列中适当的位置
for (int i = 1; i < n; i++) {
//从a[i - 1]开始向a[0]方向扫描各元素,寻找适当位置插入a[i]
int j = i;
T temp = a[i];
while (j > 0 && temp < a[j - 1]) {
//逐个比较,直到temp >= a[j - 1]时,j便是应插入的位置。
//若达到j == 0,则0是应插入的位置。
a[j] = a[j - 1]; //将元素逐个后移,以便找到插入位置时可立即插入。
j--;
}
//插入位置已找到,立即插入。
a[j] = temp;
for(int k=0;k<n;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
}
}
int main()
{
int data[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};
insertionSort(data,20);
return 0;
}