-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
"sstream": "cpp", | ||
"stdexcept": "cpp", | ||
"streambuf": "cpp", | ||
"ctime": "cpp" | ||
"ctime": "cpp", | ||
"cstring": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
set(CMAKE_CXX_COMPILER "g++")#设置C++编译器 | ||
set(CMAKE_CXX_FLAGS "-g -Wall") | ||
|
||
set(CMAKE_CXX_STANDARD 17) # 使用C++17标准 | ||
|
||
PROJECT (proj CXX) | ||
ADD_EXECUTABLE(main main.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include<iostream> | ||
|
||
class BaseClass{ | ||
private: // 类内代码才能访问 | ||
int age; | ||
protected: // | ||
int level; | ||
public: | ||
int width, height; | ||
void coutAge(){ | ||
std::cout << "age = " << age << std::endl; | ||
} | ||
void coutLevel(){ | ||
std::cout << "level = " << level << std::endl; | ||
} | ||
void coutWH(){ | ||
std::cout << "width = " << width << "\nheight = " << height << std::endl; | ||
} | ||
BaseClass(){ | ||
age = 31; | ||
level = 4; | ||
width = 4; | ||
height = 5; | ||
} | ||
|
||
// 声明友元函数, 不能直接在类内定义,只能在类内声明,在内外定义 | ||
// 友元函数不是这个类的成员变量,所以不受public等的影响 | ||
// 友元函数可以访问类的private成员变量 | ||
friend void printAge(BaseClass bc); | ||
}; | ||
|
||
// 将父类的所有成员都继承下来,创建派生类对象时,会创建一个新的内存储存这些继承的成员变量/函数 | ||
class Son1 : public BaseClass{ | ||
public: | ||
void coutAge2(){ | ||
// // 这里的age继承自基类,储存在派生类对象的内存中,但是由于其来自基类并且访问权限是private,所以无法直接访问,但可以通过继承来的基类的public函数访问 | ||
// std::cout << "coutAge2 func, age = " << age << std::endl; // 报错:member "BaseClass::age" (declared at line 5) is inaccessible | ||
|
||
|
||
std::cout << "this is coutAge2 in Class Son1 " << std::endl; | ||
coutAge(); | ||
} | ||
|
||
}; | ||
|
||
void printAge(BaseClass bc){ | ||
std::cout << "bc.age:" << bc.age << std::endl; | ||
} | ||
|
||
int main(){ | ||
// BaseClass bc(); // 这个容易和类声明混淆 | ||
// Son1 sn(); | ||
|
||
|
||
BaseClass bc; | ||
Son1 sn; | ||
|
||
|
||
// std::cout << bc.age << std::endl; //无法获取age | ||
// std::cout << sn.age << std::endl; //基类中有private age,派生类中可以继承,但是无法直接访问 | ||
|
||
|
||
sn.coutAge(); | ||
sn.coutAge2(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# include<iostream> | ||
|
||
int main(){ | ||
std::cout << "3Darray equal to 2Darray" << std::endl; | ||
int arr1[2][3][4] = {0,1,2,3,4,5,6,7,8}; | ||
int (*arr2)[3][4] = &arr1[0]; | ||
int *arr3[6]; | ||
std::cout << typeid(&arr3).name() << std::endl; // PA6_Pi 指向shape为6的int*数组的指针 | ||
std::cout << typeid(arr3).name() << std::endl; // A6_Pi 指向shape为6的int*数组 | ||
std::cout << typeid(&arr1[0]).name() << std::endl; // PA3_A4_i int (*)[3][4] | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
set(CMAKE_CXX_COMPILER "g++") #设置C++编译器 | ||
set(CMAKE_CXX_FLAGS "-g -Wall") | ||
|
||
set(CMAKE_CXX_STANDARD 17) # 使用C++17标准 | ||
|
||
PROJECT (proj CXX) | ||
SET(SRC_LIST main.cpp) | ||
ADD_EXECUTABLE(main ${SRC_LIST}) | ||
|
||
MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR}) | ||
MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
|
||
// return 引用 | ||
int& fun(){ | ||
static int qa = -111; | ||
std::cout << qa << std::endl; | ||
return qa; // 返回指向qa的引用 | ||
} | ||
|
||
// return 值 | ||
int fun2(){ | ||
static int qb = -111; | ||
std::cout << qb << std::endl; | ||
return qb; // 先拷贝qb的值到临时变量,随后return此临时变量到主函数 | ||
} | ||
|
||
int main(){ | ||
fun() = 335; | ||
fun(); | ||
// int& a = 334; // 引用a无法绑定int常量 | ||
return 0 ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
set(CMAKE_CXX_COMPILER "g++") #设置C++编译器 | ||
set(CMAKE_CXX_FLAGS "-g -Wall") | ||
|
||
set(CMAKE_CXX_STANDARD 17) # 使用C++17标准 | ||
|
||
PROJECT (proj CXX) | ||
SET(SRC_LIST main.cpp) | ||
ADD_EXECUTABLE(main ${SRC_LIST}) | ||
|
||
MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR}) | ||
MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
struct Books{ | ||
char title[50] = "Default Title"; // 默认title | ||
char author[50]; | ||
char subject[100]; | ||
int book_id; | ||
}; | ||
|
||
typedef struct Books Books; | ||
|
||
typedef struct | ||
{ | ||
char title[50]; | ||
char author[50]; | ||
char subject[100]; | ||
int book_id; | ||
}defBooks; | ||
|
||
|
||
int main(){ | ||
Books Book1, Book2; | ||
|
||
// Book1 详述 | ||
strcpy( Book1.title, "Learn C++ Programming"); | ||
strcpy( Book1.author, "Chand Miyan"); | ||
strcpy( Book1.subject, "C++ Programming"); | ||
Book1.book_id = 6495407; | ||
|
||
// Book2 详述 | ||
strcpy( Book2.title, "Telecom Billing"); | ||
strcpy( Book2.author, "Yakit Singha"); | ||
strcpy( Book2.subject, "Telecom"); | ||
Book2.book_id = 6495700; | ||
|
||
// 输出 Book1 信息 | ||
std::cout << "Book 1 title : " << Book1.title <<std::endl; | ||
std::cout << "Book 1 author : " << Book1.author <<std::endl; | ||
std::cout << "Book 1 subject : " << Book1.subject <<std::endl; | ||
std::cout << "Book 1 id : " << Book1.book_id <<std::endl; | ||
|
||
// 输出 Book2 信息 | ||
std::cout << "Book 2 title : " << Book2.title <<std::endl; | ||
std::cout << "Book 2 author : " << Book2.author <<std::endl; | ||
std::cout << "Book 2 subject : " << Book2.subject <<std::endl; | ||
std::cout << "Book 2 id : " << Book2.book_id <<std::endl; | ||
|
||
return 0; | ||
} |