类
#include <iostream>
class Course {
private:
std::string name;
public:
Course(std::string name) :name(name) {}
};
class People {
private:
std::string name;
int age = 0;
public:
People(std::string name, int age) :name(name), age(age) {}
};
class Student : public People{
private:
Course course;
public:
Student(std::string name, int age, Course course1, std::string courseName) :People(name, age), course(courseName) {}
};
int main() {
Course course("C++");
Student student("zhangSan", 23, course, "c++++++");
return 0;
}
构造函数
构造函数 方式1
#include <iostream>
class People {
public:
People() {
std::cout << "空参构造函数" << std::endl;
}
People(char * name) :People(name, 0){
std::cout << "1个参数沟通函数." << name << std::endl;
}
People(char * name, int age) :name(name), age(age) {
std::cout << "2个参数构造函数." << name << ", " << age << std::endl;
}
private:
char * name;
int age;
public:
char * getName(){
return this->name;
}
int getAge() {
return this->age;
}
void setName(char * name) {
this->name = name;
}
void setAge(int age){
this->age = age;
}
};
int main() {
People people;
people.setName("zhangSan");
people.setAge(23);
std::cout<< people.getName() << ", " << people.getAge() << std::endl;
std::cout<< " ------------------ " << std::endl;
People people2("liSi", 24);
std::cout<< people2.getName() << ", " << people2.getAge() << std::endl;
std::cout<< " ------------------ " << std::endl;
People people3("wangWu");
std::cout<< " ------------------------------------ " << std::endl;
People * people4 = new People("liuLiu", 26);
std::cout<< people4->getName() << ", " << people4->getAge() << std::endl;
delete people4;
return 0;
}
构造函数 方式2
CMakeLists.txt
People.h
#include <iostream>
class People {
public:
People();
People(char * name);
People(char * name, int age);
private:
char * name;
int age;
public:
char * getName();
int getAge();
void setName(char * name);
void setAge(int age);
};
People.cpp
#include "People.h"
People::People() {
std::cout << "empty parameter" << std::endl;
}
People::People(char *name) :People(name, 0){
this->name = name;
std::cout << "one parameter." << name << std::endl;
}
People::People(char *name, int age) {
this->name = name;
this->age = age;
std::cout << "two parameter." << name << ", " << age << std::endl;
}
char * People::getName(){
return this->name;
}
int People::getAge() {
return this->age;
}
void People::setName(char * name) {
this->name = name;
}
void People::setAge(int age){
this->age = age;
}
main.cpp
#include "People.h"
int main() {
People people;
people.setName("zhangSan");
people.setAge(23);
std::cout<< people.getName() << ", " << people.getAge() << std::endl;
std::cout<< " ------------------ " << std::endl;
People people2("liSi", 24);
std::cout<< people2.getName() << ", " << people2.getAge() << std::endl;
std::cout<< " ------------------ " << std::endl;
People people3("wangWu");
std::cout<< " ------------------------------------ " << std::endl;
People * people4 = new People("liuLiu", 26);
std::cout<< people4->getName() << ", " << people4->getAge() << std::endl;
delete people4;
people4 = NULL;
return 0;
}
析构函数
CMakeLists.txt
People.h
#include <iostream>
class People {
public:
People();
People(char * name);
~People();
private:
char * name;
public:
char * getName();
void setName(char * name);
};
People.cpp
#include "People.h"
People::People() {
std::cout << "empty parameter" << std::endl;
}
People::People(char *name) {
this->name = name;
std::cout << "one parameter." << name << std::endl;
}
People::~People() {
std::cout << "~People." << std::endl;
}
char * People::getName(){
return this->name;
}
void People::setName(char * name) {
this->name = name;
}
main.cpp
#include "People.h"
int main() {
People * people = new People("liuLiu");
std::cout<< people->getName() << std::endl;
delete people;
people = NULL;
return 0;
}
构造函数和析构函数调用顺序
#include <iostream>
class Person {
public:
char * name;
Person(char * name) : name(name) {
std::cout << "Person构造函数" << std::endl;
}
~Person() {
std::cout << "Person析构函数" << std::endl;
}
};
class Student : public Person {
public:
char * name;
Student(char * name) : Person(name) {
std::cout << "Student构造函数" << std::endl;
}
~Student() {
std::cout << "Student析构函数" << std::endl;
}
};
int main() {
Student student("Derry");
return 0;
}
友元函数
友元函数1
#include <iostream>
class People {
private:
int age = NULL;
public:
People(int age) :age(age){}
int getAge() {
return this->age;
}
friend void updateAge(People * people, int age);
};
void updateAge(People * people, int age) {
people->age = age;
}
int main() {
People people1 = People(23);
updateAge(&people1, 88);
std::cout << "people1.getAge(): " << people1.getAge() << std::endl;
return 0;
}
友元函数2
CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(mycpptest)
set(CMAKE_CXX_STANDARD 14)
add_executable(mycpptest main.cpp People.cpp People.h)
People.h
#include <iostream>
class People {
private:
int age = NULL;
public:
People(int age);
int getAge();
friend void updateAge(People * people, int age);
};
People.cpp
#include "People.h"
People::People(int age) {
this->age = age;
}
int People::getAge() {
return this->age;
}
void updateAge(People * people, int age) {
people->age = age;
}
main.cpp
#include "People.h"
int main() {
People people1 = People(23);
updateAge(&people1, 88);
std::cout << "people1.getAge(): " << people1.getAge() << std::endl;
return 0;
}
友元类
#include <iostream>
class People {
private:
int age = NULL;
friend class Class;
};
class Class {
public:
People people;
void changeAge(int age) {
people.age = age;
}
int getAge() {
return people.age;
}
};
int main() {
Class peopleClass;
peopleClass.changeAge(22);
std::cout << peopleClass.getAge() << std::endl;
return 0;
}
运算符重载
类外运算符重载
#include <iostream>
class People {
private:
int number1, number2;
public:
People(int number1, int number2) :number1(number1), number2(number2) {}
void setNumber1(int number1) {
this->number1 = number1;
}
void setNumber2(int number2) {
this->number2 = number2;
}
int getNumber1(){
return this->number1;
}
int getNumber2(){
return this->number2;
}
};
People operator + (People people1, People people2) {
int number1 = people1.getNumber1() + people2.getNumber1();
int number2 = people1.getNumber2() + people2.getNumber2();
People peo(number1, number2);
return peo;
}
int main() {
People people1(10, 20);
People people2(30, 40);
People people = people1 + people2;
std::cout << people.getNumber1() << ", " << people.getNumber2()<< std::endl;
return 0;
}
类内运算重载
#include <iostream>
class People {
private:
int number1, number2;
public:
People(int number1, int number2) :number1(number1), number2(number2) {}
void setNumber1(int number1) {
this->number1 = number1;
}
void setNumber2(int number2) {
this->number2 = number2;
}
int getNumber1(){
return this->number1;
}
int getNumber2(){
return this->number2;
}
People operator + (const People & people){
int num1 = this->number1 + people.number1;
int num2 = this->number2 + people.number2;
return People(num1 ,num2);
}
void operator ++ () {
this->number1 = this->number1 + 1;
this->number2 = this->number2 + 1;
}
void operator ++ (int) {
this->number1 = this->number1 + 1;
this->number2 = this->number2 + 1;
}
};
int main() {
People people1(10, 20);
People people2(30, 40);
People people = people1 + people2;
std::cout << people.getNumber1() << ", " << people.getNumber2()<< std::endl;
People people3(100, 200);
people3++;
++people3;
std::cout << people3.getNumber1() << ", " << people3.getNumber2()<< std::endl;
return 0;
}
继承
#include <iostream>
class People {
private:
char * name;
int age = NULL;
public:
People(char * name, int age) :name(name), age(age) {
std::cout << "Person 构造函数" << std::endl;
}
void print() {
std::cout << this->name << ", " << this->age << std::endl;
}
};
class Student : public People{
private:
char * address;
public:
Student(char * name, int age, char * address) :People(name, age) , address(address){
std::cout << "Student 构造函数" << std::endl;
}
void test() {
print();
}
};
int main() {
Student student("zhangSan", 21, "xxx");
student.test();
return 0;
}
多继承1
#include <iostream>
class People1 {
public:
void show(){
std::cout << "People1 show" << std::endl;
}
void play(){
std::cout << "People1 play" << std::endl;
}
};
class People2 {
public:
void study(){
std::cout << "People2 study" << std::endl;
}
void play(){
std::cout << "People2 play" << std::endl;
}
};
class Student : public People1, public People2{
public:
void study(){
std::cout << "Student study" << std::endl;
}
};
int main() {
Student student;
student.study();
student.People1::play();
student.People2::play();
return 0;
}
多继承2
#include <iostream>
class Object {
public:
int number;
};
class People1 : public Object{
};
class People2 : public Object{
};
class Student : public People1, public People2{
public:
int number;
};
int main() {
Student student;
student.People1::number;
student.People2::number;
student.number;
return 0;
}
虚基类
#include <iostream>
class Object {
public:
int number;
};
class People1 : virtual public Object{
};
class People2 : virtual public Object{
};
class Student : public People1, public People2{
};
int main() {
Object object;
People1 people1;
People2 people2;
Student student;
object.number = 1;
people1.number = 2;
people2.number = 3;
student.number = 4;
std::cout << object.number << std::endl;
std::cout << people1.number << std::endl;
std::cout << people2.number << std::endl;
std::cout << student.number << std::endl;
return 0;
}
多态
动态多态
动态多态:程序在运行期间才能确定调用哪个类的函数 C++ 默认关闭多态,加上virtual即可
#include <iostream>
class BaseActivity {
public:
virtual void onStart() {
std::cout << "BaseActivity onStart" << std::endl;
}
};
class HomeActivity : public BaseActivity{
public:
void onStart() {
std::cout << "HomeActivity onStart" << std::endl;
}
};
class LoginActivity : public BaseActivity{
public:
void onStart() {
std::cout << "LoginActivity onStart" << std::endl;
}
};
void startToActivity(BaseActivity * baseActivity) {
baseActivity->onStart();
}
int main() {
HomeActivity * homeActivity = new HomeActivity();
LoginActivity * loginActivity = new LoginActivity();
startToActivity(homeActivity);
startToActivity(loginActivity);
BaseActivity * baseActivity1 = new HomeActivity();
BaseActivity * baseActivity2 = new LoginActivity();
startToActivity(baseActivity1);
startToActivity(baseActivity2);
return 0;
}
静态多态
静态多态 : 编译期已经决定,调用哪个函数 重载(静态多态)
#include <iostream>
void add(int number1, int number2) {
std::cout << number1 + number2 << std::endl;
}
void add(float number1, float number2) {
std::cout << number1 + number2 << std::endl;
}
void add(double number1, double number2) {
std::cout << number1 + number2 << std::endl;
}
int main() {
add(1, 2);
add(1.1f, 2.1f);
add(1.2, 1.3);
return 0;
}
纯虚函数(似java接口)
纯虚函数必须被继承,否则子类就是抽象类
#include <iostream>
class BaseActivity {
public:
void onCreate() {
getLayoutID();
initView();
initData();
}
virtual std::string getLayoutID() = 0;
virtual void initView() = 0;
virtual void initData() = 0;
};
class MainActivity : public BaseActivity {
std::string getLayoutID() {
return "xxxxx";
}
void initView() {}
void initData() {}
};
int main() {
MainActivity mainActivity;
return 0;
}
全纯虚函数(似java接口)
#include <iostream>
class Student {
public:
std::string name;
int age;
};
class IStudent {
public:
virtual void insert(Student student) = 0;
virtual void query(Student student) = 0;
};
class Student_Impl : public IStudent {
public:
void insert(Student student) {
std::cout << "insert" << std::endl;
}
void query(Student student) {
std::cout << "query" << std::endl;
}
};
int main() {
Student * student = new Student();
student->name = "zhangSan";
student->age = 23;
Student_Impl studentImpl;
studentImpl.insert(*student);
return 0;
}
回调
#include <iostream>
class ILoginResponse {
public:
virtual void success(int code, char * msg) = 0;
virtual void error(int code, char * msg) = 0;
};
class ILoginResponseImpl : public ILoginResponse {
public:
void success(int code, char * msg) {
std::cout << code << ", " << msg << std::endl;
}
void error(int code, char * msg) {
std::cout << code << ", " << msg << std::endl;
}
};
void loginAction(std::string userID, std::string pwd, ILoginResponse & callback) {
if (userID.empty() || pwd.empty()){
std::cout << "userID | pwd is empty!" << std::endl;
return;
}
if (userID == "zhangSan" && pwd == "123") {
callback.success(0, "login success");
} else {
callback.success(-1, "login fail");
}
}
int main() {
ILoginResponseImpl callback;
loginAction("zhangSan", "12312", callback);
return 0;
}
this
#include <iostream>
class People {
private:
char * name;
public:
static int id;
public:
void setName(char * name){
this->name = name;
}
char * getName(){
return this->name;
}
};
int People::id = 1001;
int main() {
People people1;
people1.setName("zhangSan1");
people1.id = 2002;
People people2;
people2.setName("zhangSan2");
people2.id = 3003;
std::cout << "people1.getName(): " << people1.getName() << ", id: " << people1.id << std::endl;
std::cout << "people1.getName(): " << people2.getName() << ", id: " << people2.id << std::endl;
std::cout << "People::id: " << People::id << std::endl;
return 0;
}
static
#include <iostream>
class People {
public:
char * name;
static int age;
void update1() {
age += 1;
}
static void update2() {
age += 1;
}
};
int People::age = 9;
int main() {
People people;
std::cout << People::age << std::endl;
People::update2();
people.update2();
people.update1();
std::cout << People::age << std::endl;
return 0;
}
|