niusouti.com

请问下述代码中: int operator+(…)起什么作用?this 是什么?ccc 的值最终为多少?class Fruit{public:Fruit(){weight = 2;}Fruit(int w){weight = w;}int operator+(Fruit f){return this->weight * f.weight;}private:int weight;};Fruit aaa;Fruit bbb(4);int ccc = aaa + bbb;

题目

请问下述代码中: int operator+(…)起什么作用?this 是什么?ccc 的值最终为多少?

class Fruit

{

public:

Fruit()

{

weight = 2;

}

Fruit(int w)

{

weight = w;

}

int operator+(Fruit f)

{

return this->weight * f.weight;

}

private:

int weight;

};

Fruit aaa;

Fruit bbb(4);

int ccc = aaa + bbb;


相似考题

4.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehiele类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将Vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。include<iostream.h>class vehicle{private:int MaxSpeed;int Weight;public://*************found************vehicle(int maxspeed,int weight):——~vehicle{};int getMaxSpeed{return MaxSpeed;}int getWeight{retum Weight;}};//****************found************class bicycle:——public vehicle{private:int Height;public:bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}int getHeight{retum Height;};};//*******************found**************class motorcar:——public vehicle{private:int SeatNum;public:motorcar(int maxspeed。int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}int getSeatNum{return SeatNum;};};//*****************found***************class motorcycle:——{public:motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}};void main{motorcycle a(80,150,100);cout<<a.getMaxSpeed<<endl;cout<<a.getWeight<<endl;cout<<a.getHeight<<endl;cout<<a.getSeatNum<<endl;}

更多“请问下述代码中: int operator+(…)起什么作用?this 是什么?ccc 的值最终为多少?class Fruit{public:Fruit(){weight = 2;}Fruit(int w){weight = w;}int operator+(Fruit f){return this-weight * f.weight;}private:int weight;};Fruit aaa;Fruit bbb(4);int ccc = aaa + bbb;”相关问题
  • 第1题:

    若有以下程序:include using namespace std;class A{private:int x;public:int z;voi

    若有以下程序:#include <iostream>using namespace std;class A{private: int x;public: int z; void setx(int i) { x=i; } int getx() { return x; }};class B: public A{private: int m;public: int p; void setvalue(int a, int b, int c) { setx(a); z=b; m=c; } void display() { cout<<getx()<<","<<z<<","<<m<<end1; }};int main(){ B obj; obj.setvalue(2,3,4); obj.display(); return 0;程序运行以后的输出结果是( )

    A.产生语法错误

    B.2,3,4

    C.2,2,2

    D.4,3,2


    正确答案:B

  • 第2题:

    有以下程序:include using namespace std;class A{private: int x,y;public: void se

    有以下程序: #include <iostream> using namespace std; class A { private: int x,y; public: void set (int i,int j) { x=i; y=j; } int get_y() { return y; } }; class box { private: int length,width; A label; public: void set(int 1,int w, int s,int p) { length=1; width=w; label.set(s,p); } int get_area() { return length*width; } }; int main() { box small; small.set(2,4,1,35); cout<<small.get_area()<<end1; return 0; } 运行后的输出结果是( )。

    A.8

    B.4

    C.35

    D.70


    正确答案:A
    解析:本题考核成员对象的应用。类box的成员函数set()为设置对象的坐标值和对象的长、宽值。成员函数setarea返回该对象的面积。程序最后输出为8。

  • 第3题:

    调用函数bbb后,输出是什么

    void ccc(int x)

    {

    char szTemp[10] = "";

    x = 2;

    sprintf(szTemp, "%d,", x);

    afxDump << szTemp;

    if(x = 3)

    {

    int x = 4;

    sprintf(szTemp, "%d,", x);

    afxDump << szTemp;

    }

    sprintf(szTemp, "%d,", x);

    afxDump << szTemp;

    }

    void bbb()

    {

    char szTemp[10] = "";

    int x = 7;

    ccc(x);

    sprintf(szTemp, "%d,", x);

    afxDump << szTemp;

    }


    正确答案:
     

  • 第4题:

    请解释“func”为何种类型,这种类型的作用什么,变量ttt 的值是多少?

    typedef int (*func)(int, int*);

    int xxx(int a, int *p)

    {

    return a + *p;

    }

    int dowork(func aaa, int bbb, int *ccc)

    {

    return aaa(bbb, ccc);

    }

    int sss = 4;

    int ttt = dowork(&xxx, 3, &sss);


    正确答案:
     

  • 第5题:

    有下列程序:includeUsing namespace std;Class Amount{ int amount;public; Amount(i

    有下列程序: #include<iostream> Using namespace std; Class Amount{ int amount; public; Amount(int n=O):amount(n){} Int getAmount()const{return amount;} Amount &operator+=(AmountA) {

    A.*this

    B.this

    C.&amount

    D.amount


    正确答案:D
    解析: 此题考查的是“+”运算符重载和this指针。语句amount+=a.amount;实现3和7的求和,得到amount=10,要使程序的输出结果为10,需要把amount的值作为函数的返回值,所以横线处应填入amot。

  • 第6题:

    若有以下程序:includeusing namespace std;class A{private:int x;public:int z;void

    若有以下程序:#include<iostream>using namespace std;class A {private: int x;public: int z; void setx(int i) { x=i; } int getx () { return x; }}:class B : public A{private: int m;public: int p; void setvalue(int a, int b, int c) { setx(a) ; z=b; m=c; } void display{) { cout<<getx ()<<", "<<z<<", "<<m<<end1; }};int main(){ B obj; obj. setvalue(2,3,4); obj.display(); return 0;} 程序运行以后的输出结果是

    A.产生语法错误

    B.2,3,4

    C.2,2,2

    D.4,3,2


    正确答案:B
    解析:本题考核继承与派生。当类的继承方式为公有继承时,基类的公有成员和保护成员分别作为派生类的公有成员和保护成员,派生类的其他成员可以直接访问它们。其他外部使用者只能通过派生类的对象访问继承来的公有成员。在本题中,数据成员z和函数setx都是基类A的公有成员,它们经过公有继承以后,在派生类B中还是公有成员,而派生类B中的函数setvalue和display都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是输出已设置的各成员的值。

  • 第7题:

    下列对shell变量FRUIT操作,正确的是()

    • A、为变量赋值:$FRUIT=apple
    • B、显示变量的值:fruit=apple
    • C、显示变量的值:echo $FRUIT
    • D、判断变量是否有值:[-f“$FRUIT”]

    正确答案:C

  • 第8题:

    Given:  1. public class Method Over {  2. public void set Var (int a, int b, float c) {  3. }  4. }   Which two overload the set Var method()?

    • A、 private void set Var(int a, float c, int b) {}
    • B、 protected void set Var(int a, int b, float c) {}
    • C、 public int set Var(int a, float c, int b) {return a:}
    • D、 public int set Var(int a, int b, float c) {return a:}
    • E、 protected float set Var(int a, int b, float c) {return c:}

    正确答案:A,C

  • 第9题:

    public class Score implements Comparable {  private int wins, losses;  public Score(int w, int 1) { wins = w; losses = 1; }  public int getWins() { return wins; }  public int getLosses() { return losses; }  public String toString() {  return “<“ + wins + “,“ + losses + “>”; }  // insert code here  }  Which method will complete this class?() 

    • A、 public int compareTo(Object o) {/*mode code here*/}
    • B、 public int compareTo(Score other) {/*more code here*/}
    • C、 public int compare(Score s1,Score s2){/*more code here*/}
    • D、 public int compare(Object o1,Object o2){/*more code here*/}

    正确答案:B

  • 第10题:

    public class MethodOver  {  public void setVar (int a, int b, float c)  {  }  }   Which two overload the setVar method?()  

    • A、 Private void setVar (int a, float c, int b)  { }
    • B、 Protected void setVar (int a, int b, float c) { }
    • C、 Public int setVar (int a, float c, int b) (return a;)
    • D、 Public int setVar (int a, int b, float c) (return a;)
    • E、 Protected float setVar (int a, int b, float c) (return c;)

    正确答案:A,C

  • 第11题:

    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()

    • A、 public class Circle implements Shape { private int radius; }
    • B、 public abstract class Circle extends Shape { private int radius; }
    • C、 public class Circle extends Shape { private int radius; public void draw(); }
    • D、 public abstract class Circle implements Shape { private int radius; public void draw(); }
    • E、 public class Circle extends Shape { private int radius;public void draw() {/* code here */} }
    • F、 public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }

    正确答案:B,E

  • 第12题:

    单选题
    Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java serialization and given:  import java.io.*;  class Food implemertts Serializable {int good = 3;}  class Fruit externds Food {int juice = 5;}  public class Banana extends Fruit {  int yellow = 4;  public static void main(String [] args) {  Banana b = new Banana(); Banana b2 = new Banana();  b.serializeBanana(b); // assume correct serialization  b2 = b.deserializeBanana(); // assume correct  System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);  }  // more Banana methods go here  }  What is the result?()
    A

     restore 400

    B

     restore 403

    C

     restore 453

    D

     Compilation fails.

    E

     An exception is thrown at runtime.


    正确答案: B
    解析: 暂无解析

  • 第13题:

    下列选项成员变量声明正确的是

    A.public protected final int i;

    B.abstract class F1{…}

    C.private double height;

    D.double weight{}


    正确答案:C
    解析:本题考查对成员变量的声明。成员变量的声明格式位:修饰符type变量名;其中type可以是java语言中的任意数据类型,而修饰符可以是public、protected, private,static,final,transient,volatile等。选项A错误,成员变量不能同时声明成 public和protected。选项B是类的声明格式,并不是成员变量的声明。成员变量声明应以“;”结尾,选项D错误。选项C声明了一个私有的double型成员变量,为正确答案。

  • 第14题:

    下列成员变量声明中,正确的是______。

    A.public protected final int i;

    B.abstract class F1{…}

    C.private double height;

    D.double weight


    正确答案:C
    解析: 成员变量的修饰符可以是public、protected、private、static、final、transient、volatile等,选项A错误。成员变量不能同时声明成public和protected。选项B是类的声明格式,并不是成员变量的声明。成员变量声明应以“;”结束,选项D不正确。选项C声明一个私有的double型成员变量,此为正确答案。

  • 第15题:

    int func1(int& b)

    {

    return 0;

    }

    void func2()

    {

    int bbb = 3;

    func1(&bbb);

    func1(bbb);

    }

    func2中有何错误,func1的参数b 的类型是什么。


    正确答案:
     

  • 第16题:

    下列中 a的值是_________

    #define AAA 200

    #define BBB AAA+100

    int a= BBB*2


    正确答案:
     

  • 第17题:

    以下关于Python自带数据结构的运算结果中正确的是哪一项?

    A.l = [1, 2, 3, 4, 5]; del l[2:4]; 则运算之后l为[1,2, 3]。

    B.basket = ['apple', 'banana', 'apple', 'orange'] ;fruit = set(basket);fruit2 = set(['apple', 'melo']); len(fruit |fruit2) 的结果是5。

    C.basket = ['apple', 'banana', 'apple', 'orange'] ;fruit = set(basket); len(fruit) 的运算结果是4。

    D.l = [2, 1, 3, 5, 4]; l.remove(3); l.sort(); 则运算之后l为[1, 2, 4, 5]


    正确答案:D

  • 第18题:

    下列对shell变量FRUIT操作,正确的是()

    A.为变量赋值:$FRUIT=apple

    B.显示变量的值:fruit=apple

    C.显示变量的值:echo $FRUIT

    D.判断变量是否有值:[-f“$FRUIT”]


    参考答案:C

  • 第19题:

    class A {  protected int method1(int a, int b) { return 0; }  }  Which two are valid in a class that extends class A?() 

    • A、 public int method1(int a, int b) { return 0; }
    • B、 private int method1(int a, int b) { return 0; }
    • C、 private int method1(int a, long b) { return 0; }
    • D、 public short method1(int a, int b) { return 0: }
    • E、 static protected int method1(int a, int b) { return 0; }

    正确答案:A,C

  • 第20题:

    Assuming that the serializeBanana2() and the deserializeBanana2() methods will correctly use Java serialization and given:  import java.io.*;  class Food {Food() { System.out.print(”1”); } }  class Fruit extends Food implements Serializable {  Fruit() { System.out.print(”2”); } }  public class Banana2 extends Fruit { int size = 42;  public static void main(String [] args) {  Banana2 b = new Banana2();  b.serializeBanana2(b); // assume correct serialization  b = b.deserializeBanana2(b); // assume correct  System.out.println(” restored “+ b.size + “ “); }  // more Banana2 methods  }  What is the result?() 

    • A、 Compilation fails.
    • B、 1 restored 42
    • C、 12 restored 42
    • D、 121 restored 42
    • E、 1212 restored 42
    • F、 An exception is thrown at runtime.

    正确答案:D

  • 第21题:

    11. class Payload {  12. private int weight;  13. public Payload(int wt) { weight = wt; }  13. public void setWeight(mt w) { weight = w; }  15. public String toString { return Integer.toString(weight); }  16. }  17.  18. public class TestPayload {  19. static void changePayload(Payload p) { 20. /* insert code here */ 21. }  22.  23. public static void main(String[] args) {  24. Payload p = new Payload();  25. p.setWeight(1024);  26. changePayload(p);  27. System.out.println(”The value of p is “+ p);  28. }  29. }  Which statement, placed at line 20, causes the code to print “The value of p is 420.”?() 

    • A、 p.setWeight(420);
    • B、 p.changePayload(420);
    • C、 p = new Payload(420);
    • D、 Payload.setWeight(420);
    • E、 p = Payload.setWeight(420);
    • F、 p = new Payload(); p.setWeight(420);

    正确答案:A

  • 第22题:

    Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java serialization and given:  import java.io.*;  class Food implemertts Serializable {int good = 3;}  class Fruit externds Food {int juice = 5;}  public class Banana extends Fruit {  int yellow = 4;  public static void main(String [] args) {  Banana b = new Banana(); Banana b2 = new Banana();  b.serializeBanana(b); // assume correct serialization  b2 = b.deserializeBanana(); // assume correct  System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);  }  // more Banana methods go here  }  What is the result?() 

    • A、 restore 400
    • B、 restore 403
    • C、 restore 453
    • D、 Compilation fails.
    • E、 An exception is thrown at runtime.

    正确答案:C

  • 第23题:

    1. public class Blip {  2. protected int blipvert(int x) { return 0; }  3. }  4. class Vert extends Blip {  5. // insert code here  6. }  Which five methods, inserted independently at line 5, will compile?()  

    • A、 public int blipvert(int x) { return 0; }
    • B、 private int blipvert(int x) { return 0; }
    • C、 private int blipvert(long x) { return 0; }
    • D、 protected long blipvert(int x, int y) { return 0; }
    • E、 protected int blipvert(long x) { return 0; }
    • F、 protected long blipvert(long x) { return 0; }
    • G、protected long blipvert(int x) { return 0; }

    正确答案:A,C,D,E,F