注册 登录
编程论坛 C++教室

初学者,大神报大腿,求教下面题目

grosjean 发布于 2016-03-24 15:51, 5757 次点击
三、下列的类声明程序片段是否有语法错误,如有请改正(每题5分, 共10分)
1.    class LINE {
 public:
     int draw();
     int is_on_line(int x, int y);
 private:
     int start_x = 0, start_y = 0;
     int end_x, end_y;
 };

2.    class SQUARE {
 public:
     void SQUARE(int x = 0, int y = 1, int len = 1);
     ~SQUARE();
     int draw();
 private:
     int left_up_x, left_up_y;
     int side_len;
 };

四、写出程序运行结果(每题15分, 共30分)
1.
#include <iostream.h>
void fun(int a[],int n,int &s){
s=0;
for(int i=0;i<n;i+=2)
s+=a[i];
}
void main(){
int x[5]={1,2,3,4,5};
int y[10]={1,2,3,4,5,6,7,8,9,10};
int m,n;
fun(x,5,m);
cout<<"m="<<m<<endl;
fun(y,8,m);
cout<<"m="<<m<<endl;
fun(y+3,5,m);
cout<<"m="<<m<<endl;
}
 
2.
#include<iostream.h>
class Base
{
public:
    virtual void fn()
    {
        cout<<"In Base class \n";
    }
};
class SubClass:public Base
{
public:
    virtual void fn()
    {
        cout<<"In SubClass \n";
    }
};
void test(Base & b)
{
    b.fn();
}
void main()
{
    Base bc;
    SubClass sc;
    cout<<"Calling test(bc)\n";
    test(bc);
    cout<<"Calling test(sc)\n";
    test(sc);
}

五、按照要求补充下面程序的空白处(每题15分,共15分)
#include <iostream.h>
class Point {   
private:     
int x, y;   
public:     
Point(int a=0, int b=0) {x=a;  y=b; }     
Point(Point &p);      
int Getx( ) {return x; }     
int Gety( ) {return y; }     
void Show( ){cout<<"x="<<x<<", y="<<y<<endl; }     
~point( ){ }
};  
//定义复制构造函数     
Point :: Point(Point &p)      
{_________(1)__________}  
//Point 类对象作为函数的形参     
void display(__________(2)___________)      
{___________(3)_______________}  
//Point 类对象的引用作为函数的形参     
void disp(___________(4)___________)     
{__________(5)___________}
//函数的返回值为 Point 类的对象
Point fun( )     {         
Point a(50, 20);           
return a;
}
void main( )  {
Point a(3, 2);      
Point b(a);      
Point c(5, 8);      
disp(b);      
c=fun();      
disp(c);  
}
输出的结果如下:
x=3, y=2
x=50, y=20

六、设计一个平面几何中的位置类POSITION,该类至少提供移动、求到另一点距离(包括到原点距离)、求x坐标、求y坐标等操作(每题15分,共15分)
18 回复
#2
grosjean2016-03-24 15:52
小弟感激不尽
#3
grosjean2016-03-24 16:36
顶一下,谢各位
#4
grosjean2016-03-24 16:43
顶一下
#5
wengbin2016-03-24 16:51
1.    class LINE {
 public:
     int draw();
     int is_on_line(int x, int y);
     LINE();//构造函数是必须要有的
 private:
     int start_x = 0, start_y = 0;
     int end_x, end_y;
 };

2.    class SQUARE {
 public:
     void SQUARE(int x = 0, int y = 1, int len = 1);//改为SQUARE(int x = 0, int y = 1, int len = 1);构造函数是没有返回类型的
     ~SQUARE();
     int draw();
 private:
     int left_up_x, left_up_y;
     int side_len;
 };

四、写出程序运行结果(每题15分, 共30分)
1.
#include <iostream.h>
void fun(int a[],int n,int &s){
s=0;
for(int i=0;i<n;i+=2)
s+=a[i];//奇位数字之和给s
}
void main(){
int x[5]={1,2,3,4,5};
int y[10]={1,2,3,4,5,6,7,8,9,10};
int m,n;
fun(x,5,m);//m=9
cout<<"m="<<m<<endl;//m=9\n
fun(y,8,m);
cout<<"m="<<m<<endl;//m=24\n
fun(y+3,5,m);
cout<<"m="<<m<<endl;//m=18\n
}
 输出:
m=9
m=16
m=18

2.
#include<iostream.h>
class Base
{
public:
    virtual void fn()
    {
        cout<<"In Base class \n";
    }
};
class SubClass:public Base
{
public:
    virtual void fn()
    {
        cout<<"In SubClass \n";
    }
};
void test(Base & b)
{
    b.fn();
}
void main()
{
    Base bc;
    SubClass sc;
    cout<<"Calling test(bc)\n";
    test(bc);
    cout<<"Calling test(sc)\n";
    test(sc);
}
输出:
Calling test(bc)
In Base class
Calling test(sc)
In SubClass

五、按照要求补充下面程序的空白处(每题15分,共15分)
#include <iostream.h>
class Point {   
private:     
int x, y;   
public:     
Point(int a=0, int b=0) {x=a;  y=b; }     
Point(Point &p);      
int Getx( ) {return x; }     
int Gety( ) {return y; }     
void Show( ){cout<<"x="<<x<<", y="<<y<<endl; }     
~point( ){ }
};  
//定义复制构造函数     
Point :: Point(Point &p)      
{_________(1)x=p.x;y=p.y;__________}  
//Point 类对象作为函数的形参     
void display(__________(2)Point p___________)      
{___________(3)cout<<"x="<<p.Getx()<<", y="<<pGety()<<endl;_______________}  //有更新
//Point 类对象的引用作为函数的形参     
void disp(___________(4)Point &p___________)     
{__________(5)cout<<"x="<<p.Getx()<<", y="<<pGety()<<endl;___________} //有更新
//函数的返回值为 Point 类的对象
Point fun( )     {         
Point a(50, 20);           
return a;
}
void main( )  {
Point a(3, 2);      
Point b(a);      
Point c(5, 8);      
disp(b);      
c=fun();      
disp(c);  
}
输出的结果如下:
x=3, y=2
x=50, y=20

[此贴子已经被作者于2016-3-24 17:11编辑过]

#6
wengbin2016-03-24 16:52
想问一下这些题是干嘛的?面试题目?
#7
grosjean2016-03-24 16:57
回复 6楼 wengbin
笔试题目,不是学计算机的根本不会
#8
grosjean2016-03-24 16:57
回复 5楼 wengbin
非常感谢
#9
wengbin2016-03-24 17:10
回复 7楼 grosjean
面试哪家单位?是找实习嘛?
#10
grosjean2016-03-24 17:24
回复 5楼 wengbin
2.写出下列程序的运行结果。
#include <iostream.h>
class A{
private:
int a;
public:
A(int aa=0){a=aa;}
~A(){cout<<”Destructor A!”<<a<<endl;}
};
class B : public A {
private:
int b;
public:
B(int aa=0,int bb=0):A(aa){b=bb;}
~B(){cout<<”Destructor B!”<<b<<endl;}
};
void main(){
B x(8),y(6,7);
}


3.下面程序用于计算各类形状的总面积,请对各括号中的内容予以填充。
#include <iostream.h>
class shape {   
public:________(1)___________
float total( _________(2)_________, int n)      
{ float sum =0;      
for(int i= 0; i< n; i++)
sum+= s[i] -> area( );         
return sum;     
}  
};
class tri: public shape {   
protected: float H, W;   
public:      
tri(float h, float w) {H = h; W = w;}      
float area( )      
{return H* W*0.5; }   
};
class rec: public tri  {   
public:     
rec(float h, float w) :tri(h, w) { }     
float area( ) { return H * W; }  
};
main( ) {      
shape *s[2];     
s[0]= ________(3)_________tri(3.0, 4.0);     
s[l]=__________(4)_________rec(2.0, 4.0);     
float sum = total(s, ________(5)__________ );   
}

还有两题不会的,麻烦解答,很感谢你
#11
wengbin2016-03-24 17:44
1:
B7
A6
B0
A8
#12
grosjean2016-03-25 08:58
回复 11楼 wengbin
谢谢!!
#13
小舛2016-03-25 17:01
#include<stdio.h>
int main()
{
    int a,b,c;
    printf("请输入三角形的边:");
    scanf("%d,%d,%d\n",&a,&b,&c);
    if ((a+b)<=c&&(b+c)<=a&&(a+b)<=c)
       printf("这不是一个三角形\n");
    else if(a==b&&b==c&&c==a)   printf("这是一个等边三角形\n");
            else if(a==b||b==c||a==c)   printf("这是一个等腰三角形\n");
                 else if(a!=b&&b!=c&&c!=a)    printf("这是一个一般三角形\n");
    return 0;
}帮帮忙

#14
小舛2016-03-25 17:02
回复 13楼 小舛
哪里错 指教下
#15
winnie962016-03-26 20:47
回复 5楼 wengbin
版主大大,在没有定义构造函数的时候,系统会生成 默认构造函数 吧!!
#16
winnie962016-03-26 20:51
回复 13楼 小舛
你的逻辑错了
#17
winnie962016-03-26 20:53
回复 13楼 小舛
#include<stdio.h>
int main()
{
    int a,b,c;
    printf("请输入三角形的边:");
    scanf("%d,%d,%d\n",&a,&b,&c);
    if ((a+b)<=c&&(b+c)<=a&&(a+b)<=c)
       printf("这不是一个三角形\n");
    else if(a==b&&b==c&&c==a)   printf("这是一个等边三角形\n");
            
                 else if(a!=b&&b!=c&&c!=a)    printf("这是一个一般三角形\n");
                 else  printf("这是一个等腰三角形\n");
    return 0;
}
#18
c9742884322016-04-12 16:45
Destructor B! 7
Destructor A! 6
Destructor B! 0
Destructor A! 8
#19
c9742884322016-04-13 17:19
#include <iostream.h>
class shape {   
public: virtual float area()=0;
float total( shape *s[], int n)      
{ float sum =0;      
for(int i= 0; i< n; i++)
sum+= s[i] -> area( );         
return sum;     
}  
};
class tri: public shape {   
protected: float H, W;   
public:      
tri(float h, float w) {H = h; W = w;}      
float area( )      
{return H*W*0.5; }   
};
class rec: public tri  {   
public:     
    rec(float h, float w):tri(h, w) { }     
float area( ) { return H * W; }  
};
main( ) {      
shape *s[2];     
s[0]=&tri(3.0, 4.0);     
s[1]=&rec(2.0, 4.0);     
float sum = s[0]->total(s,2); //这里  

}
1