您好,欢迎来到钮旅网。
搜索
您的当前位置:首页C++实验

C++实验

来源:钮旅网
二、实验内容和原理

(1)do-while语句编程,求自然数1-10的和;并用for语句改写此程序。

(2)编程计算图形的面积。程序可计算圆形、长方形、正方形的面积,运行时先提求用户选择图形的类型,然后,再要求用户对圆形输入半径值,对长方形输入长与宽,对正方形输入边长,计算出面积的值后,在屏幕上显示出来。 四、操作方法与实验步骤

// 实验一.cpp : #include \"stdafx.h\" #include\"iostream\" using namespace std;

int _tmain(int argc, _TCHAR* argv[]) { int i=1,sum=0; do { }

sum=sum+i;

i++;

}while(i<11);

cout<<\"自然数到的和是:\"<// 实验二.cpp : 定义控制台应用程序的入口点。 #include \"stdafx.h\" #include\"iostream\" using namespace std; void menu();

int _tmain(int argc, _TCHAR* argv[]) {

int n,m=1; menu();

while(m=1){

} }

cout<<\"请选择要面积的图形\"<>n; switch(n){ case 1:{ double Radius;

cout<<\"请输入圆的半径:\"<>Radius;

cout<<\"圆的面积为:\"<<3.14*Radius*Radius<case 2:{ double Length , width;

cout<<\"请输入长方形的长和宽:\"<>Length >> width;

cout<<\"长方形的面积为:\"<case 3:{double Length; cout<<\"请输入正方形的边长:\"<cin>>Length ; cout<<\"正方形的面积为:\"<case 0: exit(0); break; }

return 0;

void menu(){

cout<<\"┅┅┅┅┅┅.圆\"<二、实验内容和原理

(1)设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角的两个点的坐标,能计算矩形的面积。要求定义构造函数与析构函数。

(2)编写重载函数MAX1可分别求取2个整数、3个整数、2个双精度数、3个双精度数的最大值。 四、实验步骤

// 实验一.cpp :

class Rectangle { // Rectangle.h private : double a,b; public : };

#include \"stdafx.h\" #include\"iostream\" using namespace std; #include\"Rectangle.h\"

int _tmain(int argc, _TCHAR* argv[]) { Rectangle ld(1,3); Rectangle ru(3,4); }

ld.print1(); ru.print2();

cout<<\"长方形的面积为\"<return 0;

Rectangle(double m,double n){ a=m;b=n;

cout<<\"construction\"<void print1(){cout<<\"长方形左下角点的坐标:(\"<double Rectangle::area(Rectangle p1){

return (p1.a-a)*(p1.b-b); }

Rectangle::~Rectangle (){ cout<<\"destruction\"<int MAXI(int a,int b);

int MAXI(int a,int b,int c);

double MAXI(double a,double b);

double MAXI(double a,double b,double c);

int _tmain(int argc, _TCHAR* argv[]) { int a,b,c;

cout <<\"请输入两个数a,b\"<>a>>b;

cout<<\"较大的数是:\"<>a>>b>>c;

cout<<\"较大的数是\"<cout<<\"请输入两个数m,n\"<>m>>n;

cout <<\"较大的数是:\"<>m>>n>>k;

cout<<\"较大的数是:\"<return 0;

}

int MAXI(int a,int b){ return a>b?a:b;

}

double MAXI(double a,double b){

return a>b?a:b;}

int MAXI(int a,int b,int c){ int max;

max=a>b?a:b; max=max>c?max:c;

return max;}

double MAXI(double a,double b,double c){ double max; max=a>b?a:b;

max=max>c?max:c; return max; }

二、实验内容和原理

(1)声明一个基类BaseClass,有整数类型成员变量Number,构造基派生类DerivedClass,实现其构造函数和析构函数,完善类的功能与结构。 (2)声明一个基类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square;完善类的功能与结构。 三、主要仪器设备 计算机

Visual Studio 2008 四、操作方法与实验步骤

// 实验一.cpp : #include \"stdafx.h\" #include \"iostream\" using namespace std; class base{ private: int Number; public:

base(int i):Number(i){ cout<<\"base construction.....\"<~base(){cout<<\"base destruction...\"<} };

class derive: public base{ private: int no; public:

derive(int n):no(n),base(0){cout<<\"derive construction...\"<~derive(){cout<<\"derive destruction ....\"<int _tmain(int argc, _TCHAR* argv[]) { base b(90); b.print(); derive d1(23);

d1.print(); derive d2(12);

d2.print(); return 0; }

void base::print(){

cout<<\"base 的值为\"<cout<<\"derive 的值为\"<// 实验二.cpp : #include \"stdafx.h\" #include\"iostream\" using namespace std; class SHAPE{

public: double GetArea(){return 0;} };

class Rectangle:virtual public SHAPE { private :

double Length , width; public: Rectangle(double l,double w):Length(l),width(w){ };

void print(){cout<<\"长方形的长和宽为\"<};

double GetArea(){return Length*width;}

class Circle:virtual public SHAPE { private :

double Radius; public: };

class Square:virtual public Rectangle{ private:

double len; public: Square(double l):Rectangle(0,0),len(l){} void print(){cout<<\"正方形的边长为\"<int _tmain(int argc, _TCHAR* argv[]) { Rectangle r(3,4); r.print();

cout<<\"长方形的面积\"<cout<<\"圆的面积\"<s.print();

cout<<\"正方形的面积\"<}

double GetArea(){return len*len;}

Circle(double r):Radius(r){ };

void print(){cout<<\"圆的半径为\"<return 0; }

二、实验内容和原理

(1)编写一个抽象类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。计算周长的函数GetPerim();完善类的功能与结构。

(2)声明一个车(Vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(Bicycle)类、汽车(Motorcar)类,从(Bicycle)和(Motorcar)派生出摩托车(Motorcycle)类,它们都有Run、Stop等成员函数。利用虚函数解决问题。 三、主要仪器设备 计算机

Visual Studio 2008 四、实验步骤

// 实验一.cpp : #include \"stdafx.h\" #include\"iostream\" using namespace std;

class SHAPE{ public: virtual double GetArea() const=0{return 0;} };

class Rectangle:virtual public SHAPE { private :

double Length , width;

public: Rectangle(double l,double w):Length(l),width(w){ };

void print(){cout<<\"长方形的长和宽为\"<class Circle:virtual public SHAPE { private : double Radius;

public: Circle(double r):Radius(r){ };

void print(){cout<<\"圆的半径为\"<double GetArea() const{return Radius*Radius*3.14;} double GetPerim(){return 2*3.14*Radius;}

};

int _tmain(int argc, _TCHAR* argv[]) {Rectangle r(3,4); r.print();

cout<<\"长方形的面积\"<cout<<\"长方形的周长为\"<c.print();

cout<<\"圆的面积\"<return 0; }

// 实验二.cpp : //

#include \"stdafx.h\" #include \"iostream\" using namespace std; class Vehicle{ public: };

class Bicycle:virtual public Vehicle{ public:

void Run(){cout<<\"Bicycle run\"<void Stop(){cout<<\"Bicycle stop\"<class Motorcar: virtual public Vehicle{ public:

void Run(){cout<<\"Motorcar run\"<public: void Run(){cout<<\"Motorcycle run\"<void Stop(){cout<<\"Motorcycle stop\"<int _tmain(int argc, _TCHAR* argv[]) { Bicycle b;

b.Run(); b.Stop(); Motorcar l;

l.Run(); l.Stop();

Motorcycle m; m.Run(); }

m.Stop(); return 0;

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- niushuan.com 版权所有 赣ICP备2024042780号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务