看板 YP90-304 關於我們 聯絡資訊
#include<iostream.h> class house { private: int length,width; public: house(); //constructer void area(); }; house::house() { cout<<"Please enter length:"; cin>>length; cout<<"Please enter width:"; cin>>width; } void house::area() {cout<<"Area="<<length*width<<endl;} void main() { house H; //compiler會暗自呼叫constructer H.area(); } 1.若把area宣告在class外,請在class內宣告area()為house的"friend" 2.如果classs內只有變數,無函式,請用"struct",千萬別用class且還把在class內的 變數的type宣告為public阿,因為c++的compiler在製造上為了保持和c的相容性, 故早已把"struct"當作class,而其中的變數型態也預設為"public" 還有如果王八要問我運算子多載也行 ,繼承 多型 模板隨你問 won ba~~~~~~~~~~~~bell -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.231.49.138