浙大 C++
本机环境
> g++ -v
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
第一个 C++ 程序
touch main.cpp
#include <iostream>
using namespace std;
int main(void) {
cout << "Hello world! I am " << 18 << " today!" << endl;
return 0;
}
g++ main.cpp
./a.out
What is an object
- Object = Entity
- Objects = Atrributes + Services
- Data
- Operations
- in C language
#include <stdio.h>
typedef struct point3d{
float x;
float y;
float z;
}Point3;
void Point3Print(const Point3 *p);
int main(void) {
Point3 a;
a.x = 1;
a.y = 2;
a.z = 3;
Point3Print(&a);
return 0;
}
void Point3Print(const Point3 *p){
printf("(%f,%f,%f)",p->x,p->y,p->z);
}
- in c++
#include <iostream>
using namespace std;
class Point3{
private:
float x;
float y;
float z;
public:
Point3(float x, float y, float z);
void Point3Print();
};
Point3::Point3(float x, float y, float z){
this->x=x;
this->y=y;
this->z=z;
}
void Point3::Point3Print(){
cout << "x=" << x << ",y=" << y << ",z=" << z << endl;
}
int main(void) {
Point3 a(1, 2, 3);
a.Point3Print();
return 0;
}
头文件
- Declarations vs Definitions (声明 vs 定义)
- A .cpp file is a compile unit
- Only declaraions are allowed to be in .h
- extern variables
- function prototypes
- class/struct declaration
- #include
- #include "xxx" : first search in the current directory,then the directories delared somewhere
- #include <xxx> : search in the
speified directories
- 条件编译
标准头文件结构
#ifndef _MY_H_
#define _MY_H_
void hello();
#endif //_MY_H_
- extern int gl;
仅是告诉编译器在某个地方有一个变量叫 gl ;具体定义这个变量的地方暂时可以不知道。
成员变量
class A{
public:
int i;
void f();
};
void A::f(){
cout << i;
}
int main(){
A a;
a.i = 10;
a.f();
}
构造和析构
class A{
public:
A();
~A();
void f();
};
A::A(){
cout << "init" << endl;
}
A::~A(){
cout << "destory" << endl;
}
void A::f(){
cout << "a f() is run" << endl;
}
int main(void) {
cout << "main begin" << endl;
{
cout << "local begin" << endl;
A a;
a.f();
cout << "local end" << endl;
}
cout << "main end" << endl;
return 0;
}
对象初始化
- struct
struct Y
{
float f;
int a;
Y(int a);
};
Y::Y(int a){}
int main(void) {
Y y1[] = {Y(1), Y(2), Y(3)};
Y y2[2] = {Y(11)}; // error
return 0;
}
- class
class Y
{
public:
Y(int a);
};
Y::Y(int i){}
int main(void) {
Y y1[] = {Y(1), Y(2), Y(3)};
Y y2[2] = {Y(11)}; // error
return 0;
}
fix
class Y
{
public:
Y();
Y(int a);
};
Y::Y(int i){}
Y::Y(){}
int main(void) {
Y y1[] = {Y(1), Y(2), Y(3)};
Y y2[2] = {Y(11)}; // compile pass
return 0;
}
new & delete
- dynamic memory allocation
- new
- new int;
- new A;
- new int[10];
- delete
- delete p;
- delete[] arr;
//
int * i = new int;
delete i;
//
int * psome = new int[10];
delete[] psome;
//
A *a = new A();
delete a;
A *r = new A[10];
delete[] r;
访问限制
- 关键字
- private
- no one can access that member except inside funcion members of that type.
- 这个类的成员函数有访问权限
- public
- protected
class A{
private:
int i;
public:
void set(int i){this->i = i;}
void g(A* a){cout << a->i << endl;} // private 的权限限制仅仅在编译时刻
};
int main(void) {
A a;
a.set(100);
A b;
b.g(&a);
return 0;
}
- friend
- 你可以声明别人是你的朋友,然后它就可访问你的私有变量了
- 可以访问朋友的私有变量
struct X; // Declaration
struct Y
{
void f(X*);
};
struct X{ // Definition
private:
int i;
public:
void init();
friend void g(X*,int); // Global friend
friend void Y::f(X*); // Struct member friend
friend struct Z; // Entire struct is a friend
friend void h();
};
void X::init(){
i = 333;
}
void g(X* x,int i){
x->i = i; // 在 X 中被定义为 friend ,可以访问私有变量
}
void Y::f(X* x){
// 在 X 中被定义为 friend ,可以访问私有变量
cout << x->i << endl;
}
struct Z{
};
void h(){
X x;
x.i = 200; // 在 X 中被定义为 friend ,可以访问私有变量
cout << x.i << endl;
}
void m(){
X x;
// x.i = 200; error 没有定义为 friend ,不能直接访问私有变量
}
int main(void) {
X x;
x.init();
// cout << x.i; error 不能直接访问私有变量
Y y;
y.f(&x);
g(&x, 100);
y.f(&x);
h();
return 0;
}
- class vs. struct
in c++ , struct is class, but
- class default to private
- struct default to public
- 首选 class
初始化列表
- 早于构造函数的执行
- 案例
class A{
private:
int i, j;
public:
A():i(100),j(200){ cout << "A init" << endl;}
void f() { cout << "A::i=" << i << " j=" << j << endl;};
};
- Initialization vs. assignment
class Student{
private:
string name;
public:
Student(string);
void printName() { cout << "Student::name=" << name << endl;};
};
// assignment
// inside constructor
// string must have a default constructor
// Student::Student(string s){
// name = s;
// }
// initialization
// before constructor
// 推荐用法
Student::Student(string s):name(s){
}
int main(void) {
Student a("zhangsan");
a.printName();
return 0;
}