{ }
cout<<\int pos,n,l;
string str = \cout< cout<<\cin>>pos; if( pos<1 || pos>l ) { cout<<\ return 0; } cout<<\cin>>n; if( n<1 || pos+n>l ) { cout<<\ return 0; } string str2=str.substr(pos,n); cout< cout<<\return 0; 14) 选做:定义一个Point(二维点类)的对象数组,利用该数组实现直线的线性拟合。 注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。 #ifndef _POINT_H #define _POINT_H class Point { public: Point (float x=0,float y=0) : x(x),y(y){} float getX() const {return x;}; float getY() const {return y;}; private: float x,y; }; #endif #include\#include float lineFit(const Point points[],int nPoint) { float avgX=0,avgY=0; float lxx=0,lyy=0,lxy=0; int i=0; for( i=0; i int main() { 注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。 } Point p[10]={Point(6,10),Point(3,5),Point(12,10),Point(6,5),Point(23,46), Point(4,29),Point(12,32),Point(84,90),Point(100,100),Point(73,88)}; float r = lineFit(p,10); cout<<\return 0; 15) 选做:定义一个动态数组类。 16) 思考并回答:数组,指针,对象数组,动态内存分配,默认构造函数,标准类库,字符串类 string,线性拟合。 数组:是具有一定顺序的若干对象的集合体,(相同类型变量的集合体) 指针:通过指针得到存储地址 对象数组:数组里的每个元素都是类的对象,赋值时先定义对象,然后直接将对象直接赋值给数组 动态内存分配:在程序执行中按照实际需要申请适量的内存,使用后还可以释放 默认构造函数:类中没有写构造函数,编译器自动生成一个默认构造函数 标准类库:C++标准库里用来存储和处理数据的类的集合 字符串类 string:提供对字符串进行处理所需要的操作 线性拟合:求线的特性 3. 主要仪器设备及软件:Windows 2000+VC 6.0 实验四 继承与派生 (设计性实验 2学时) 1. 目的要求: 1) 学习定义和使用类的继承关系,定义派生类;熟悉不同继承方式下对基类成员的访问控制; 2) 学习利用虚基类解决二义性问题。 注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。 2. 实验内容: 17) 定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把 age改为公有成员变量,还会有问题吗?编程试试看。 #include class Animal { public: }; void Animal::SetAge(int n) { } class dog:public Animal { public: }; int main() { Animal(){cout<<\构造函数调用!!!\< ~Animal(){cout<<\析构函数调用 !!!\< private: age = n; cout<<\< dog(){cout<<\构造函数调用!!!\< ~dog(){cout<<\析构函数调用!!!\< age = n; cout<<\< private: Animal A; dog d; A.SetAge(3); d.SetAge(5); return 0; 注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。 } 在其成员函数SetAge(int n)中直接给age赋值,程序不会有问题, 把 age改为公有成员变量,也没有问题 不过当age为私有成员变量的时候不能通过对象打点的形式在主函数中赋值,而为公有成员变量是便可以。 18) 定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,定义该派生类的对象,观察构造函数和析构函数的执行情况。 #include public: BaseClass(){cout<<\构造函数调用!!!\ ~BaseClass(){cout<<\析构函数调用!!!\private: int Number; }; class BaseClass1 { public: BaseClass1(int i){cout<<\构造函数调用!!!\ \ ~BaseClass1(){cout<<\析构函数调用!!!\private: int Number; }; class DerivedClass:public BaseClass,public BaseClass1 注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。