cpp

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <iostream>
#include <stdarg.h>

//========================命名空間========================
//標準命名空間(包含很多標準的定義)
//standard
using namespace std;
//命名空間類似於Java中包(歸類)


/*
//自定義命名空間
namespace NSP_A{
int a = 9;
struct Teacher{
char name[20];
int age;
};
struct Student{
char name[20];
int age;
};

}


namespace NSP_B{
int a = 12;
//命名空間嵌套
namespace NSP_C{
int c = 90;
}
}


void main(){
//運算符重載
//std::cout << "this is c plus plus" << std::endl;
cout << "this is c plus plus" << endl;
//使用命名空間
//::訪問修飾符
cout << NSP_A::a << endl;
cout << NSP_B::a << endl;


cout << NSP_B::NSP_C::c << endl;


//使用命名空間中的結構體
using NSP_A::Student;
Student t;
t.age = 90;


system("pause");
}
*/


/*
#define PI 3.14
//圓
class MyCircle{
//屬性(共用權限訪問修飾符)
private:
double r;
double s;
public:
void setR(double r){
this->r = r;
}
//獲取面積
double getS(){
return PI * r * r;
}
};


void main(){
MyCircle c1;
c1.setR(4);


cout << "圓的面積:" << c1.getS() << endl;


system("pause");
}
*/
//結構體
/*
struct MyTeacher{
public:
char name[20];
int age;
public:
void say(){
cout << this->age << "歲" << endl;
}
};
void main(){
MyTeacher t1;
t1.age = 10;
t1.say();
system("pause");
}
*/


//布爾類型
/*
void main(){
//bool isSingle = true;
bool isSingle = 17;
//false -17


if (isSingle){
cout << "單身" << endl;
cout << sizeof(bool) << endl;
}
else{
cout << "有對象" << endl;
}


int a = 10, b = 20;
((a > b) ? a : b) = 30;
cout << b << endl;


system("pause");
}
*/


//引用
/*
void main(){
//變量名-門牌號(內存空間0x00001的別名,可不可以有多個名字?)
int a = 10;
//b就這個內存空間另外一個別名\
//& C++中的引用
int &b = a;
cout << b << endl;


system("pause");
}
*/


/*
//指針值交換
void swap_1(int *a, int *b){
int c = 0;
c = *a;
*a = *b;
*b = c;
}


//引用值交換
void swap_2(int &a, int &b){
int c = 0;
c = a;
a = b;
b = c;
}


void main(){
int x = 10;
int y = 20;

printf("%d,%d\n",x,y);
//swap_1(&x, &y);
//a成了x的別名
swap_2(x,y);
printf("%d,%d\n", x, y);


system("pause");
}
*/


/*
struct Teacher{
char* name;
int age;
};


void myprint(Teacher &t){
cout << t.name << "," << t.age << endl;
t.age = 21;
}


void myprint2(Teacher *t){
cout << t->name << "," << t->age << endl;
//(*t).name 
}


//引用的主要功能:作爲函數的參數或返回值
void main(){
Teacher t;
t.name = "Jason";
t.age = 20;
myprint(t);


myprint2(&t);


system("pause");
}
*/


//指針的引用,代替二級指針
/*
struct Teacher{
char* name;
int age;
};


void getTeacher(Teacher **p){
Teacher *tmp = (Teacher*)malloc(sizeof(Teacher));
tmp->age = 20;
*p = tmp;
}


//指針的引用,代替二級指針
//Teacher* &p = (Teacher * *p)
void getTeacher(Teacher* &p){
p = (Teacher*)malloc(sizeof(Teacher));
p->age = 20;
}


void main(){
Teacher *t = NULL;


getTeacher(&t);


system("pause");
}
*/


//指針常量與常量指針
/*
void main(){
//指針常量,指針的常量,不改變地址的指針,但是可以修改它指向的內容
int a = 2, b = 3;
int *const p1 = &a;
//p1 = &b;  //NO
*p1 = 4;


//常量指針,指向常量的指針,內容不能修改
const int *p2 = &a;
p2 = &b;
//*p2 = 9;  //NO
}
*/


//1.單純給變量取別名沒有任何意義,作爲函數參數傳遞,能保證參數傳遞過程中不產生副本
//2.引用可以直接操作變量,指針要通過取值(*p),間接操作變量,指針的可讀性差


/*
//常引用類似於java中final
void myprintf(const int &a){
cout << a << endl;
}


void main(){
//const int a;
//引用必須要有值,不能爲空
//int &a = NULL;


//常引用
int a = 10, b = 9;
const int &c = a;


//字面量
const int &d = 70;


//c = b;
myprintf(c);

system("pause");
}
*/




//引用的大小
/*
struct Teacher{
char name[20];
int age;
};


void main(){
Teacher t;


Teacher &t1 = t;
Teacher *p = &t;


cout << sizeof(t1) << endl;
cout << sizeof(p) << endl;
system("pause");
}
*/


/*
struct Teacher{
char name[20];
int age;
};


void myprint(Teacher *t){
cout << t->name << "," << t->age << endl;
}


void myprint2(Teacher &t){
cout << t.name << "," << t.age << endl;
t.age = 21;
}


void main(){
Teacher t;


Teacher *p = NULL;
//報錯,防止不報錯,進行非空判斷
myprint(p);


//引用不能爲空,沒法傳進去
Teacher &t2 = NULL;
myprint2(t2);


system("pause");
}
*/




//函數默認參數
/*
void myprint(int x, int y = 9, int z = 8){
cout << x << endl;
}

//重載
void myprint(int x,bool ret){
cout << x << endl;
}


void main(){
myprint(20);


system("pause");
}
*/


//可變參數
//int...
/*
void func(int i,...)
{
//可變參數指針
va_list args_p;
//開始讀取可變參數,i是最後一個固定參數
va_start(args_p,i);
int a = va_arg(args_p,int);
char b = va_arg(args_p, char);
int c = va_arg(args_p, int);
cout << a << endl;
cout << b << endl;
cout << c << endl;
//結束
va_end(args_p);
}




void main(){
func(9,20,'b',30);


system("pause");
}
*/


//循環讀取
/*
void func(int i,...)
{
//可變參數指針
va_list args_p;
//開始讀取可變參數,i是最後一個固定參數
va_start(args_p,i);
int value;
while (1){
value = va_arg(args_p,int);
if (value <= 0){
break;
}
cout << value << endl;
}


//結束
va_end(args_p);
}


void main(){
func(9, 20, 40, 30);


system("pause");
}
*/


//C++類的普遍寫法
/*
#include "MyTeacher.h"


void main(){
MyTeacher t1;
t1.name = "Jack";
t1.age = 20;


cout << t1.getName() << endl;


system("pause");
}
*/


//構造函數、析構函數、拷貝構造函數
/*
class Teacher{
private:
char *name;
int age;
public:
//無參構造函數(寫了,就會覆蓋默認的無參構造函數)
Teacher(){
cout << "無參構造函數" << endl;
}
//有參構造函數會覆蓋默認的構造函數
Teacher(char *name, int age){
this->name = name;
this->age = age;
cout << "有參構造函數" << endl;
}
};




void main(){
//Teacher t1;
Teacher t2("yuehan",20);


//另外一種調用方式
Teacher t3 = Teacher("jack",21);


system("pause");
}
*/


/*
//析構函數
class Teacher{
private:
char *name;
int age;
public:
//無參構造函數賦默認值
Teacher(){
this->name = (char*)malloc(100);
strcpy(name,"jack walson");
age = 20;
cout << "無參構造函數" << endl;
}
//析構函數
//當對象要被系統釋放時,析構函數被調用
//作用:善後處理
~Teacher(){
cout << "析構" << endl;
//釋放內存
free(this->name);
}
};


void func(){
Teacher t1;
}


void main(){
func();

system("pause");
}
*/


//拷貝構造函數
/*
class Teacher{
private:
char *name;
int age;
public:
Teacher(char *name, int age){
this->name = name;
this->age = age;
cout << "有參構造函數" << endl;
}
//拷貝構造函數(值拷貝)
//默認拷貝構造函數,就是值拷貝
Teacher(const Teacher &obj){
this->name = obj.name;
this->age = obj.age;
cout << "拷貝構造函數" << endl;
}
void myprint(){
cout << name << "," << age << endl;
}
};


Teacher func1(Teacher t){
t.myprint();
return t;
}


void main(){
Teacher t1("rose",20);


//拷貝構造函數被調用的場景
//1.聲明時賦值
//Teacher t2 = t1;
//t2.myprint();
//2.作爲參數傳入,實參給形參賦值
func1(t1);
//3.作爲函數返回值返回,給變量初始化賦值
//Teacher t3 = func1(t1);


//這裏不會被調用
//Teacher t1 ;
//Teacher t2;
//t1 = t2;


system("pause");
}
*/


//淺拷貝(值拷貝)問題
/*
class Teacher{
private:
char *name;
int age;
public:
Teacher(char *name, int age){
this->name = (char*)malloc(100);
strcpy(this->name,name);
this->age = age;
cout << "有參構造函數" << endl;
}
~Teacher(){
cout << "析構" << endl;
//釋放內存
free(this->name);
}
void myprint(){
cout << name << "," << age << endl;
}
};


void func(){
Teacher t1("rose", 20);


Teacher t2 = t1;
t2.myprint();
}


void main(){
//運行完畢會執行兩次析構函數 對同一塊內存空間free()兩次,會報錯!  解決方案-->深拷貝!
func();


system("pause");
}
*/


//深拷貝
/*
class Teacher{
private:
char *name;
int age;
public:
Teacher(char *name, int age){
int len = strlen(name);
this->name = (char*)malloc(len+1);
strcpy(this->name, name);
this->age = age;
cout << "有參構造函數" << endl;
}
~Teacher(){
cout << "析構" << endl;
//釋放內存
free(this->name);
}
//深拷貝 重寫拷貝函數  拷貝的對象指向新的內存
Teacher(const Teacher &obj){
//複製name屬性
int len = strlen(obj.name);
this->name = (char*)malloc(len+1);
strcpy(this->name,obj.name);
this->age = obj.age;
}
void myprint(){
cout << name << "," << age << endl;
}
};


void func(){
Teacher t1("rose", 20);


Teacher t2 = t1;
t2.myprint();
}


void main(){
func();


system("pause");
}
*/


//構造函數的屬性初始化列表
/*
class Teacher{
private:
char* name;
public:
Teacher(char* name){
this->name = name;
cout << "Teacher有參構造函數" << endl;
}
~Teacher(){
cout << "Teacher析構函數" << endl;
}
char* getName(){
return this->name;
}


};


class Student{
private:
int id;
//屬性對象
//Teacher t = Teacher("miss cang");
Teacher t1;
Teacher t2;
public:
Student(int id,char *t1_n, char* t2_n) : t1(t1_n), t2(t2_n){
this->id = id;
cout << "Student有參構造函數" << endl;
}
void myprint(){
cout << id << "," << t1.getName() <<"," << t2.getName() << endl;
}
~Student(){
cout << "Student析構函數" << endl;
}
};


void func(){
Student s1(10, "miss bo", "mrs liu");
//Student s2(20, "miss cang", "jason");
s1.myprint();
//s2.myprint();
}


void main(){
func();


system("pause");
}
*/


//C++ 通過new(delete)動態內存分配
//C  malloc(free)

/*
class Teacher{
private:
char* name;
public:
Teacher(char* name){
this->name = name;
cout << "Teacher有參構造函數" << endl;
}
~Teacher(){
cout << "Teacher析構函數" << endl;
}
void setName(char* name){
this->name = name;
}
char* getName(){
return this->name;
}
};


void func(){
//C++
//會調用構造和析構函數
Teacher *t1 = new Teacher("jack");
cout << t1->getName() << endl;
//釋放
delete t1;


//C
//Teacher *t2 = (Teacher*)malloc(sizeof(Teacher));
//t2->setName("jack");
//free(t2);

}


void main(){
func();
//數組類型
//C
//int *p1 = (int*)malloc(sizeof(int) * 10);
//p1[0] = 9;
//free(p1);

//C++
int *p2 = new int[10];
p2[0] = 2;
//釋放數組 []
delete[] p2;


system("pause");
}
*/


//static 靜態屬性和方法
/*
class Teacher{
public:
char* name;
//計數器
static int total;
public:
Teacher(char* name){
this->name = name;
cout << "Teacher有參構造函數" << endl;
}
~Teacher(){
cout << "Teacher析構函數" << endl;
}
void setName(char* name){
this->name = name;
}
char* getName(){
return this->name;
}
//計數,靜態函數
static void count(){
total++;
cout << total << endl;
}
};


//靜態屬性初始化賦值
int Teacher::total = 9;


void main(){
Teacher::total++;
cout << Teacher::total << endl;
//直接通過類名訪問
Teacher::count();


//也可以通過對象名訪問
Teacher t1("yuehang");
t1.count();


system("pause");
}
*/


//類的大小
/*
class A{
public:
int i;
int j;
int k;
static int m;
};


class B{
public:
int i;
int j;
int k;
void myprintf(){
cout << "打印" << endl;
}
};




void main(){
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;


//C/C++ 內存分區:棧、堆、全局(靜態、全局)、常量區(字符串)、程序代碼區
//普通屬性與結構體相同的內存佈局


//JVM Stack(基本數據類型、對象引用)
//Native Method Stack(本地方法棧)
//方法區



system("pause");
}
*/


//this,當前對象的指針
//函數是共享的,必須要有能夠標識當前對象是誰的辦法
/*
class Teacher{
private:
char* name;
int age;
public:
Teacher(char* name,int age){
this->name = name;
this->age = age;
cout << "Teacher有參構造函數" << endl;
}
~Teacher(){
cout << "Teacher析構函數" << endl;
}
//常函數,修飾的是this
//既不能改變指針的值,又不能改變指針指向的內容
//const Teacher* const this
void myprint() const{
printf("%#x\n",this);
//改變屬性的值
//this->name = "yuehang";
//改變this指針的值
//this = (Teacher*)0x00009;
cout << this->name << "," << this->age << endl;
}
void myprint2(){
cout << this->name << "," << this->age << endl;
}
};


void main(){
Teacher t1("jack",20);
const Teacher t2("rose", 18);
//t2.myprint2(); 常量對象只能調用常量函數,不能調用非常量函數
//常函數,當前對象不能被修改,防止數據成員被非法訪問
printf("%#x\n", &t1);
t1.myprint();


printf("%#x\n", &t2);
t2.myprint();


system("pause");
}
*/


//友元函數
/*
class A{
//友元函數
friend void modify_i(A *p, int a);
private:
int i;
public:
A(int i){
this->i = i;
}
void myprint(){
cout << i << endl;
}

};


//友元函數的實現,在友元函數中可以訪問私有的屬性
void modify_i(A *p, int a){
p->i = a;
}


void main(){
A* a = new A(10);
a->myprint();


modify_i(a,20);
a->myprint();


system("pause");
}
*/


/*
//友元類
class A{
//友元類

friend class B;
private:
int i;
public:
A(int i){
this->i = i;
}
void myprint(){
cout << i << endl;
}
};


class B{
public:
//B這個友元類可以訪問A類的任何成員
void accessAny(){
a.i = 30;
}
private:
A a;
};
*/


//運算符重載
/*
class Point{
public:
int x;
int y;
public:
Point(int x = 0, int y = 0){
this->x = x;
this->y = y;
}
void myprint(){
cout << x << "," << y << endl;
}
};


//重載+號
Point operator+(Point &p1, Point &p2){
Point tmp(p1.x + p2.x, p1.y + p2.y);
return tmp;
}


//重載-號
Point operator-(Point &p1, Point &p2){
Point tmp(p1.x - p2.x, p1.y - p2.y);
return tmp;
}


void main(){
Point p1(10,20);
Point p2(20,10);

Point p3 = p1 + p2;


p3.myprint();


system("pause");
}
*/


//成員函數,運算符重載
/*
class Point{
public:
int x;
int y;
public:
Point(int x = 0, int y = 0){
this->x = x;
this->y = y;
}
//成員函數,運算符重載
Point operator+(Point &p2){
Point tmp(this->x + p2.x, this->y + p2.y);
return tmp;
}
void myprint(){
cout << x << "," << y << endl;
}
};


void main(){
Point p1(10, 20);
Point p2(20, 10);


//運算符的重載,本質還是函數調用
//p1.operator+(p2)
Point p3 = p1 + p2;


p3.myprint();


system("pause");
}
*/


//當屬性私有時,通過友元函數完成運算符重載
class Point{
friend Point operator+(Point &p1, Point &p2);
private:
int x;
int y;
public:
Point(int x = 0, int y = 0){
this->x = x;
this->y = y;
}
void myprint(){
cout << x << "," << y << endl;
}
};


Point operator+(Point &p1, Point &p2){
Point tmp(p1.x + p2.x, p1.y + p2.y);
return tmp;
}


void main(){
Point p1(10, 20);
Point p2(20, 10);


//運算符的重載,本質還是函數調用
//p1.operator+(p2)
Point p3 = p1 + p2;


p3.myprint();


system("pause");
}
發佈了41 篇原創文章 · 獲贊 19 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章