c++學習(四)
阿新 • • 發佈:2018-12-18
開始涉及類和物件了
/*#include"pch.h" #include<iostream> using namespace std; #define MAXSIZE 100 #define ElemType int //typedef int ElemType; typedef struct { ElemType *elem; int length; }SqList; Status InitList(SqList ) int main() { return 0; }*/ /*#include"pch.h" #include<iostream> using namespace std; void swap(int &a, int &b) { int t = a; a = b; b = t; } int main() { int x; int y; cout << "please input your number that you want to swap:" << endl; cin >> x >> y; cout << "x=" << x << "y=" << y << endl; swap(x, y); cout << "x=" << x << "y=" << y; return 0; }*/ /*#include"pch.h" #include<iostream> using namespace std; const double PI = 3.14159265358979; inline double calArea(double radius) { return PI * radius*radius; } int main() { double r = 3.0; double area = calArea(r); cout << area << endl; return 0; }*/ /*#include"pch.h" #include<iomanip> #include<iostream> using namespace std; int getVolume(int length, int width = 2, int height = 3); int main() { int x, y, z; cout << "please input x,y,z" << endl; cin >> x >> y >> z; cout << "some body is:" << endl; cout << getVolume(x, y, z) << endl; cout << "some body data is:" << endl; cout << getVolume(x, y) << endl; cout << "some body data is:" << endl; cout << getVolume(x) << endl; return 0; } int getVolume(int length, int width, int height) { cout << setw(5) << length << setw(5) << width << setw(5) << height << '\t'; return length * width*height; }*/ //函式過載 //同樣的函式是有不同的引數型別,或者是不同的形參的個數,編譯器不以返回值型別和形參名來區別不同的函式 /*#include"pch.h" #include<iostream> using namespace std; int sumOfSquare(int a, int b) { return a * a + b * b; } double sumOfSquare(double a, double b) { return a * a + b * b; } int main() { int n, m; cout << "please input your numbers:" << endl; cin >> n >> m; cout << sumOfSquare(n, m) << endl; double x, y; cout << "please input your numbers:" << endl; cin >> x >> y; cout << sumOfSquare(x, y) << endl; return 0; }*/ /*#include"pch.h" #include<iostream> using namespace std; float Convert(float F) { float c; c = (F - 32) * 5 / 9; return c; } int main() { float x; cout << "please input your temper:" << endl; cin >> x; cout << Convert(x) << endl; return 0; }*/ /*class Clock { public: void setTime(int newH, int newM, int newS); void showTime(); private: int hour=0, minute=0, second=0;//類內初始值 };*/ //內聯成員函式 //可以提高執行的效率,對於較簡單的函式可以宣告為內聯形式 //行內函數體內不要有複雜結構 //如迴圈語句和switch 將函式放在類的宣告中,使用inline關鍵字 #include"pch.h" #include<iostream> using namespace std; class Clock { public: void setTime(int newH = 0, int newM = 0, int newS = 0); void showTime( ); private: int hour, minute, second; }; void Clock::setTime(int newH, int newM, int newS) { hour = newH; minute = newM; second = newS; } void Clock::showTime( ) { cout << hour << ":" << minute << ":" << second; } int main() { int x, y, z; cout << "please input:"; cin >> x >> y >> z; Clock myClock; myClock.setTime(x, y, z); myClock.showTime(); return 0; }