2014-04-08 107 views
-6

編寫程序以具有基類 編號 和兩個派生類: 正方形和立方體 。 你將計算平方數(A * A )和 立方數(A * A * A)爲什麼這個C++程序不能正常工作?

#include <iostream> 
#include <conio.h> 
using namespace std; 
class Number 
{ 
protected: 
    int a ; 
    int y; 
public: 
    a*a = y; 
    a*a*a=y 
}; 
class Square : Number 
{ 
    int a , y; 
public: 
    (a*a) = y; 
}; 

class Cube : Number 
{ 
    int a, y; 
public: 
    (a*a*a) = y; 
}; 
int main() 
{ 
    int a; 
    cout << "Enter number "<< endl; 
    cin >> a >>endl ; 
    cout << " the Square of this number is : " << (a*a); 
    cout << "Enter number "; 
    cin >> a; 
    cout << " the cube of this number is : " << (a*a*a); 
    return (0); 
} 
+3

因爲它甚至不是遠程有效的C++代碼嗎? –

+0

a * a = y?你想做什麼? – Bramsh

+1

你應該從一個可以工作的「hello world」程序開始,並且建立在此之上;不試圖編碼一切,然後試圖讓它工作... –

回答

4
public: 
    a*a = y; 
    a*a*a=y 

不是一個函數。

private: 
protected: 
public: 

部分定義瞭如何在類之外看到函數或變量。你不能做任何分配中他們:你需要把分配到的功能,比如像int multiply()或功能相似:

class Number 
{ 
protected: 
    int a ; 
    int y; 
public: 
    int squared(int a); 
    int cubed(int a); 
}; 

int Number::squared(int a) { 
    y = a * a; 
    return y; 
} 

int Number::cubed(int a) { 
    y = a * a * a; 
    return y; 
} 

你的編譯器錯誤是由事實來你正在做(a*a) = y,這是

  1. 的功能之外,和
  2. 錯誤地格式化爲(a*a)值分配給y。您需要改爲y = (a*a)
相關問題