请大家帮忙做些c++的题目,小弟不胜感谢

jernymy 2008-06-05 10:39:20
一、 编写程序,输出ASCII码为32-127的字符。
二、 设计并测试一个名为Rectangle的矩形类,其属性(成员数据)为矩形的左下角与右上角两点的坐标。从键盘输入两点坐标,由主程序计算并输出矩形的面积。
三、 下面的程序有问题,请仔细体会使用指针时应避免出现的这个问题。
#include<iostream.h>
int main()
{
int *P;
*P=9;
cout<<”The value at P:”<<*P;
return 0;
}
四、 设有一个point类如下:
class point
{
private:
int X, Y;
public:
point(int xx=0, int yy=0){X=xx;Y=yy;}
int GetX(){return X;}
int GetY(){return Y;}
};
编程:声明一个指向该point类的对象的指针,并用该指针来完成对象的数据输入与输出。

五、 声明一个Shape基类,有存放数据的数据成员,接收数据的函数成员。在此基础上派生出Square(矩形)和Cirlce(圆)类,二者都有GetArea( )成员函数计算对象面积。使用这两个派生类各创建一个对象,并计算各对象面积。
...全文
227 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
kunpp 2008-06-07
  • 打赏
  • 举报
回复
1.#include <iostream.h>
union pw
{
int i;
char ch[5];
};
void main()
{
pw a;
for(a.i=32;a.i<=127;a.i++)
cout<<a.ch<<" ";
}
4.#include<iostream.h>
class point
{
private:
int X,Y;
public:
point(int xx=0, int yy=0){X=xx;Y=yy;}
int GetX(){return X;}
int GetY(){return Y;}
};
void main()
{int c,d;
cin>>c>>d;
point a(c,d);
point (*b)=&a;
cout<<(*b).GetX()<<(*b).GetY()<<endl;
}
kunpp 2008-06-07
  • 打赏
  • 举报
回复
5.#include<iostream.h>
class shape
{
protected:
double a,b;
public:
shape(double x){a=x;}
shape(double x,double y){a=x;b=y;}
virtual double mian()=0;
};

class chang:public shape
{public:
chang(double a,double b):shape(a,b)
{}
double mian()
{return a*b;}
};
class yuan:public shape
{

public:
yuan(double a):shape(a)
{}
double mian()
{return 3.14*a*a;}
};
void main()
{double a,b,m;
while(1)
{
cout<<"输入长方形的长,宽:";
cin>>a>>b;
chang c(a,b);
cout<<"长方形面积:"<<c.mian()<<endl;
cout<<"输入圆的半径:";
cin>>m;
yuan y(m);
cout<<"圆的面积"<<y.mian()<<endl;
}
}
visame 2008-06-06
  • 打赏
  • 举报
回复

第3题:
#include <iostream.h>
int main()
{
int *P;//此处更改为int *p=new int;
*P=9;
cout < <”The value at P:” < <*P;
return 0;
}
MagiSu 2008-06-06
  • 打赏
  • 举报
回复
题目简单大家就没有做的激情了。
sukyin 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 morilasi 的回复:]
太简单了,建议大家不要直接给答案了,老师留的作业自己做有好处
[/Quote]
morilasi 2008-06-06
  • 打赏
  • 举报
回复
太简单了,建议大家不要直接给答案了,老师留的作业自己做有好处
jasonmike312 2008-06-06
  • 打赏
  • 举报
回复
声明一个Shape基类,有存放数据的数据成员,接收数据的函数成员。在此基础上派生出Square(矩形)和Cirlce(圆)类,二者都有GetArea( )成员函数计算对象面积。使用这两个派生类各创建一个对象,并计算各对象面积。
#include <iostream>
using namespace std;

class Shape
{//有存放数据的数据成员,接收数据的函数成员。不明白具体存放什么,没写。
public:
virtual void GetArea()=0;
};

class Square:public Shape
{
private:
double a;
double b;
public:
Square(double a,double b){this->a=a;this->b=b;};
void GetArea(){cout<<"矩形面积为:"<<a*b;};
};

class Cirlce:public Shape
{
private:
double r;
public:
Cirlce(double r){this->r=r;};
void GetArea(){cout<<"圆形面积为:"<<r*r*3.14;};
};

void main()
{
Shape *pShape[2];
pShape[0]=new Square(16.1,18.2);
pShape[1]=new Cirlce(3.0);
for(int i=0;i<2;i++)
{
pShape[i]->GetArea();
}
}
icesnowjank 2008-06-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Shape
{
public:
virtual void _draw() = 0;
virtual void _erase() = 0;
virtual ~Shape(){cout<<"~Shape()"<<endl;}
};
class ShapeFactory
{
public:
~ShapeFactory(){cout<<"~ShapeFactory()"<<endl;}
friend class ShapeFactoryInitializer;
class BadShapeCreation : public logic_error
{
public:
BadShapeCreation(string _type)
: logic_error("Can not create type:" + _type){}
};
static Shape* _CreateShape(const string& _id)
throw(BadShapeCreation)
{
if (factories.find(_id) != factories.end())
return factories[ _id ]->_Create();
else
throw BadShapeCreation(_id);
}
private:
virtual Shape* _Create() = 0;
static map<string,ShapeFactory*> factories;
};
map<string,ShapeFactory*> ShapeFactory::factories;
//////////////////////////////////////////////////////////////////////////
class Circle : public Shape
{
public:
void _draw() {cout<<"Circle::_draw()" <<endl;}
void _erase(){cout<<"Circle::_erase()"<<endl;}
~Circle(){cout<<"Circle::~Circle()"<<endl;}
private:
Circle(){}
friend class ShapeFactoryInitializer;
class Factory;
friend class Factory;
class Factory : public ShapeFactory
{
public:
Shape* _Create(){return new Circle;}
friend class ShapeFactoryInitializer;
};
};
//////////////////////////////////////////////////////////////////////////
class Square : public Shape
{
public:
void _draw() {cout<<"Square::_draw()" <<endl;}
void _erase(){cout<<"Square::_erase()"<<endl;}
~Square(){cout<<"Square::~Square()"<<endl;}
private:
Square(){}
friend class ShapeFactoryInitializer;
class Factory;
friend class Factory;
class Factory : public ShapeFactory
{
public:
Shape* _Create(){return new Square;}
friend class ShapeFactoryInitializer;
};
};
class ShapeFactoryInitializer
{
private:
static ShapeFactoryInitializer si;
ShapeFactoryInitializer()
{
ShapeFactory::factories[ "Circle" ] = new Circle::Factory;
ShapeFactory::factories[ "Square" ] = new Square::Factory;
}
~ShapeFactoryInitializer()
{
map<string,ShapeFactory*>::iterator it = ShapeFactory::factories.begin();
while (it != ShapeFactory::factories.end())
delete it ++->second;
}
};
ShapeFactoryInitializer ShapeFactoryInitializer::si;
//////////////////////////////////////////////////////////////////////////
char* s[] = {"Circle","Square","Circle","Square","Square","Circle","Square","Circle","Square","Square"};
int _tmain(int argc, char* argv[])
{
vector<Shape*> shapes;
try
{
for (int i = 0;i < sizeof(s) / sizeof(s[ 0 ]);i ++)
shapes.push_back(ShapeFactory::_CreateShape(s[ i ]));
}
catch (ShapeFactory::BadShapeCreation e)
{
cout<<e.what()<<endl;
return EXIT_FAILURE;
}
for (int i = 0;i < shapes.size();i ++)
{
shapes[ i ]->_draw();
shapes[ i ]->_erase();
}
for (vector<Shape*>::iterator _Pos = shapes.begin();_Pos != shapes.end();++ _Pos)
{
delete *_Pos;
*_Pos = NULL;
}
system("PAUSE");
return 0;
}
icansaymyabc 2008-06-06
  • 打赏
  • 举报
回复
作业题!20000日元,不讲价。
jernymy 2008-06-05
  • 打赏
  • 举报
回复
二、 设计并测试一个名为Rectangle的矩形类,其属性(成员数据)为矩形的左下角与右上角两点的坐标。从键盘输入两点坐标,由主程序计算并输出矩形的面积。
这样写ok的吧?

struct point {
int x;
int y;
};

class rectangle {
public:
struct point a;
struct point b;

int GetArea() { return( b.y - a.y ) * ( b.x - a.x));}

};
jernymy 2008-06-05
  • 打赏
  • 举报
回复
#include <iostream.h>

void main()
{
char c;
for (int i=32;i<=127;i++)
{
c=i;
cout("%c",c);
}
}
biosli 2008-06-05
  • 打赏
  • 举报
回复
又是作业题吧?
学习阶段还是自己做好...
冷月清晖 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lijiawlm 的回复:]
我晕 自己想
没一点技术含量~
[/Quote]

如:
1.

#include <stdio.h>

void main()
{
char c;
for (int i=32;i<=127;i++)
{
c=i;
printf("%c",c);
}
}
lijiawlm 2008-06-05
  • 打赏
  • 举报
回复
我晕 自己想
没一点技术含量~

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧