1. 属性初始化
第一种方式:直接在构造函数后面给对象赋值
第二种方式:在构造函数后面调对象属性构造函数进行赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <iostream> using namespace std;
class Person { protected: string name; int age; public: Person(string name, int age) :name(name), age(age) {} };
class Course { private: string name; public: Course(string name) :name(name) {} };
class Student : public Person { private: Course course; public: Student(string name, int age, Course course1, string courseNameInfo) :Person(name, age) , course(courseNameInfo) { } };
int main() { Course c("C++"); Student student("Jack", 30, c, "NDK内容真多");
return 0; }
|
2. 多态
重写属于动态多态,重载属于静态多态。同一个方法有不同的实现。
2.1 动态多态
动态多态是只有在运行时才知道调用那个方法。
Java语言默认支持多态
C++默认关闭多态,怎么开启多态? 虚函数 在父类上给函数增加 virtual关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream> using namespace std;
class BaseActivity { public: virtual void onStart() { cout << "BaseActivity onStart" << endl; } };
class HomeActivity : public BaseActivity { public: void onStart() { cout << "HomeActivity onStart" << endl; } };
class LoginActivity : public BaseActivity { public: void onStart() { cout << "LoginActivity onStart" << endl; } };
void startToActivity(BaseActivity * baseActivity) { baseActivity->onStart(); }
int main() { HomeActivity *homeActivity = new HomeActivity(); LoginActivity *loginActivity = new LoginActivity();
startToActivity(homeActivity); startToActivity(loginActivity);
if (homeActivity && loginActivity) delete homeActivity; delete loginActivity;
cout << endl;
BaseActivity * activity1 = new HomeActivity(); BaseActivity * activity2 = new LoginActivity(); startToActivity(activity1); startToActivity(activity2);
return 0; }
|
2.2 静态多态
在编译器就确定调用某个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> using namespace std;
void add(int number1, int number2) { cout << number1 + number2 << endl; }
void add(float number1, float number2) { cout << number1 + number2 << endl; }
void add(double number1, double number2) { cout << number1 + number2 << endl; }
int main() { add(10000, 10000); add(1.9f, 2.8f); add(545.4, 654.54);
return 0; }
|
3. 纯虚函数
总结:虚函数为了基类支持多态,解决C++二义性;存虚函数为了让子类实现。
在C++中不支持抽象类和接口的定义,所以就引出了纯虚函数和全纯虚函数。
纯虚函数相等于JAVA中的抽象类;全纯虚函数相当于JAVA中的接口。
纯虚函数定义最后要有 “= 0”字样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <iostream> using namespace std;
class BaseActivity { private: void setContentView(string layoutResID) { cout << "XmlResourceParser解析布局文件信息... 反射" << endl; }
public: void onCreate() { setContentView(getLayoutID());
initView(); initData(); initListener(); }
virtual string getLayoutID() = 0; virtual void initView() = 0; virtual void initData() = 0; virtual void initListener() = 0; };
class MainActivity : public BaseActivity {
string getLayoutID() { return "R.layout.activity_main"; }
void initView() { }
void initData() { }
void initListener() {
} };
int main() {
MainActivity mainActivity;
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <iostream> using namespace std;
class Student { int _id; string name; int age; };
class ISudent_DB { virtual void insertStudent(Student student) = 0; virtual void deleteStudent(int _id) = 0; virtual void updateStudent(int _id, Student student) = 0; virtual Student queryByStudent(Student student) = 0; };
class Student_DBImpl1 : public ISudent_DB { public: void insertStudent(Student student) { }
void deleteStudent(int _id) { }
void updateStudent(int _id, Student student) { }
Student queryByStudent(Student student) { } };
int main() { Student_DBImpl1 studentDbImpl1; cout << "Success" << endl; return 0; }
|
4. 模板函数
C++中模板函数非常类似JAVA中泛型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream>
using namespace std;
template <typename TT> void addAction(TT n1, TT n2) { cout << "模板函数:" << n1 + n2 << endl; }
int main() { addAction(1, 2); addAction(10.2f, 20.3f); addAction(545.34, 324.3); addAction<string>("AAA", "BBB");
return 0; }
|
5.继承关系中构造函数和析构函数调用顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream>
using namespace std;
class Person { public: string name;
Person(string name) : name(name) {cout << "Person构造函数" << endl;}
~Person() {cout << "Person析构函数" << endl;}
virtual void test() { cout << "父 test..." << endl; } };
class Student : public Person { public: string name;
Student(string name) : Person(name) { cout << "Student构造函数" << endl;
}
~Student() {cout << "Student析构函数" << endl;}
void test() { cout << "子 test..." << endl; } };
int main() { Student student("Jack");
Student student1("A"); student1.test();
return 0; }
|
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者