Skip to content

Commit

Permalink
add classpermission
Browse files Browse the repository at this point in the history
  • Loading branch information
DiaosWang committed Aug 19, 2024
1 parent a79e2a9 commit 16d2a1f
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"ctime": "cpp"
"ctime": "cpp",
"cstring": "cpp"
}
}
8 changes: 8 additions & 0 deletions ClassPermission/CMakeLists.txt
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)
67 changes: 67 additions & 0 deletions ClassPermission/main.cpp
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;
}
3 changes: 2 additions & 1 deletion array/2Darray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ int main(){


// int (*arr2)[3] = &arr1; // 报错: a value of type "int (*)[2][3]" cannot be used to initialize an entity of type "int (*)[3]".
int (*arr2)[3] = &arr1[0]; // arr2 是一个`int (*)[3]`类型指针; arr1可以表示为二维数组,但是在使用`*`索引时arr1本质上来说也是`int (*)[3]`,即指向第一行的数组.
// int (*arr2)[3] = arr1; // 这种方式可以,没有报错,并且等价于`int (*arr2)[3] = &arr1[0];`
int (*arr2)[3] = &arr1[0]; // arr2 是一个`int (*)[3]`类型指针; arr1表示为二维数组,类型为int [2][3],`&arr1`类型是`int (*)[2][3]`,也就是指向2x3int的指针(PA2_A3_i).
for(int i = 0; i < 2; i ++){
for (int j = 0; j < 3; j ++){
std::cout << arr2[i][j] << " ";
Expand Down
7 changes: 6 additions & 1 deletion array/3Darray.cpp
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;
}
12 changes: 12 additions & 0 deletions refer/CMakeLists.txt
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})
22 changes: 22 additions & 0 deletions refer/main.cpp
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 ;
}
12 changes: 12 additions & 0 deletions struct/CMakeLists.txt
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})
52 changes: 52 additions & 0 deletions struct/main.cpp
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;
}

0 comments on commit 16d2a1f

Please sign in to comment.