With C++
如何設計堆砌程式碼的方法
0
5
0
0
0
5
5
0
5
5
class Dog
{
string m_color;
public:
string furColor();
};
其他東西
Class 內部
其他東西
Class 內部
Private
Public
class Dog
{
int m_age;
int m_variety;
int calcFriendly();
public:
string bark();
int friendly();
};
class Dog
{
int m_age;
int m_variety;
int calcFriendly();
public:
string bark();
int friendly();
Dog(int age, int variety)
{
m_age = age;
m_variety = variety;
}
};
class Dog
{
int m_age;
int m_variety;
int calcFriendly();
public:
string bark();
int friendly();
Dog(int age, int variety)
{
m_age = age;
m_variety = variety;
}
};
Class預設成員皆是Private
class Matrix
{
int *data;
int row;
int column;
public:
Matrix(int r, int c);
~Matrix();
int& at(int x,int y);
};
程序導向:透過數值來達成條件判斷
物件導向:透過型態來達成條件判斷
Solo Queue
Team Work
C++ vector
Windows API
C++ basic_ios
C++ ostream
C++ fstream
#include<bits/stdc++.h>
using namespace std;
void SayHello(ostream &out)
{
out<<"hello!"<<endl;
}
int main()
{
stringstream ss;
ofstream fout("a.txt");
SayHello( cout );
SayHello(ss);
SayHello( fout );
cout<<"ss:"<<ss.str();
cout<<"fout:";
system("type a.txt");
}
繼承的妙用
父類別可以通吃所有子類別
C++的物件導向複雜程度大致上也是最龐雜的
class 子類別 : 屬性:父類別 {
};
class Base {
public:
void say()
{
cout<<"Base, Hi!"<<endl;
}
};
class Child: public Base {
public:
void child_say()
{
cout<<"Child, Hi!"<<endl;
}
};
Child c;
c.say();
c.child_say();
屬性可以重新定義父類別的成員屬性為較嚴格者
如果是public,繼承下來屬性不變
如果是private,繼承下來都是private
class Base {
public:
void say()
{
cout<<"Base, Hi!"<<endl;
}
};
class Child: public Base {
public:
void child_say()
{
cout<<"Child, Hi!"<<endl;
}
};
Child c;
c.say();
c.child_say();
Child
Base
如果一個不夠,可以試試兩個
class 子類別 : 屬性:父類別1, 屬性:父類別2 ... {
};
class Base {
public:
void say()
{
cout<<"Base, Hi!"<<endl;
}
};
class Ground {
public:
void ground_say()
{
cout<<"Ground, Hi!"<<endl;
}
};
class Child: public Base, public Ground {
public:
void child_say()
{
cout<<"Child, Hi!"<<endl;
}
};
Child
Base
Ground
class Base {
public:
void say()
{
cout<<"Base, Hi!"<<endl;
}
};
class Ground {
public:
void say()
{
cout<<"Ground, Hi!"<<endl;
}
};
class Child: public Base,public Ground {
public:
void child_say()
{
say(); // which one? or CE?
}
};
Child
Base
Ground
class 子類別 : virtual 屬性:父類別 {
};
class A {
public:
A(){ cout<<"A!"<<endl; }
};
class B : virtual public A {
public:
B(){ cout<<"B!"<<endl; }
};
class C : virtual public A {
public:
C(){ cout<<"C!"<<endl; }
};
class D : public B, public C {
public:
D(){ cout<<"D!"<<endl; }
};
如果B,C虛擬繼承A
D繼承BC的話就只有1個A