include <stdlib.h>
include <stdio.h>
include <iostream>
//
using namespace std;//標(biāo)準(zhǔn)的命名空間(包含很多標(biāo)準(zhǔn)的定義)
//// << : 運(yùn)算符重載
//
////命名空間 類似于java中的包(歸類)
//
////自定義命名空間
//namespace NSP_A{
// int a = 9;
// namespace NSP_A_SUN{
// int a = 132;
// }
// struct Teacher
// {
// int age;
// char name[20];
// };
//}
//
//namespace NSP_B{
// int a = 12;
// struct Teacher
// {
// int age;
// char name[20];
// };
//}
//void main(){
// //printf("this is C plusplus");
// cout << "this is c++" << endl;
// //使用命名空間 :: 訪問(wèn)符
// cout << NSP_A::a << endl;
// cout << NSP_A::NSP_A_SUN::a << endl;
// cout << NSP_B::a << endl;
// using NSP_A::Teacher;
// Teacher t;
// t.age = 10;
// using NSP_B::Teacher;
// Teacher t;
// t.age = 10;
// system("pause");
//}
//const double PI=3.14;
////圓
//class MyCircle{
// //屬性 (公用權(quán)限訪問(wèn)修飾符)
//private:
// double r;
// double s;
// double length;
//public:
// void setR(double r){
// this->r = r;
// }
// //獲取面積
// double getS(){
// return PIrr;
// }
//};
//void main(){
// MyCircle c1e;
// c1e.setR(4);
// cout << "圓的面積:" << c1e.getS() << endl;
//
// MyCircle c1;
// c1.setR(4);
//
// cout << "圓的面積:" << c1.getS() << endl;
// system("pause");
//}
//結(jié)構(gòu)體
//struct MyTeacher
//{
//public:
// char name[20];
// int age;
//public:
// void say(){
// cout << this->age << "歲" << endl;
// }
//};
//void main(){
// MyTeacher t;
// t.age = 12;
// t.say();
// system("pause");
//}
//void main(){
// bool isSingle = false;
// cout << isSingle << endl;
// if (isSingle > 0){
// cout << "單身" << endl;
// }else{
// cout << "有對(duì)象" << endl;
// }
// system("pause");
//}
//引用
//void main(){
// //變量名 -門牌號(hào)(內(nèi)存空間的別名 )
// int a = 10;
// //引用
// int &b = a;
// b = 20;
// cout << a << 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");
}
//指針的引用替代二級(jí)指針
//struct Teacher
//{
// char* name;
// int age;
//};
//void getTeacher(Teacher **tea){
// Teacher t = (Teacher)malloc(sizeof(Teacher));
// t->age = 13;
// tea = t;
//}
////指針的引用 替代二級(jí)指針 Teacher &tea=(Teacher p)
//void getTeacher(Teacher &tea){
// tea = (Teacher)malloc(sizeof(Teacher));
// tea->age = 14;
//}
//void main(){
// Teacher *tea = NULL;
// getTeacher(&tea);
// system("pause");
//}
//指針常量 常量指針
//void main(){
// //指針常量 指針的常量,不改變地址的指針,但是可以修改他的值
// int a = 13, b = 16;
// int *const p1 = &a;
// //p1 = &b;
// p1 = 4;
// //常量指針,指向常量的指針澈歉,內(nèi)容不能修改
// const int p2 = &a;
// p2 = &b;
// //p2 = 9;
// system("pause");
//}
//1.單純給變量取別名沒有任何意義昂勉,作為函數(shù)參數(shù)傳遞邻薯,能保證參數(shù)傳遞過(guò)程中不產(chǎn)生副本
//2.引用可以直接操作變量,指針要通過(guò)取值(p)救巷,間接操作變量默责,指針的可讀性差
//3.指針需要判空引用不需要,引用不可能為空咸包。引用不可能為空
//常引用類似于java 中的final
//常引用類似于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;
//報(bào)錯(cuò),防止不報(bào)錯(cuò)烂瘫,進(jìn)行非空判斷
myprint(p);
//引用不能為空媒熊,沒法傳進(jìn)去
Teacher &t2 = NULL;
myprint2(t2);
system("pause");
}
*/
//函數(shù)默認(rèn)參數(shù)
/*
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");
}
*/
//可變參數(shù)
//int...
//void func(int i, ...){
// ////可變參數(shù)指針
// //va_list args_p;
// //// 開始讀取可變參數(shù) i 是最后一個(gè)固定的參數(shù)
// //va_start(args_p, i);
// //int a = va_arg(args_p, int);
// //int b = va_arg(args_p, int);
// //char c = va_arg(args_p, char);
// //int d = va_arg(args_p, int);
// //int w = va_arg(args_p, int);
//
// //cout << a << endl;
// //cout << b << endl;
// //cout << c << endl;
// //cout << d << endl;
// //cout << w << endl;
// //va_end(args_p);
//
// //可變參數(shù)指針
// va_list args_p;
// //開始讀取可變參數(shù),i是最后一個(gè)固定參數(shù)
// 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;
// //結(jié)束
// va_end(args_p);
//}
//void main(){
// //func(2, 4,'b',6, 6);
// func(9, 20, 'b', 30);
// system("pause");
//}
//循環(huán)讀取
/*
void func(int i,...)
{
//可變參數(shù)指針
va_list args_p;
//開始讀取可變參數(shù)坟比,i是最后一個(gè)固定參數(shù)
va_start(args_p,i);
int value;
while (1){
value = va_arg(args_p,int);
if (value <= 0){
break;
}
cout << value << endl;
}
//結(jié)束
va_end(args_p);
}
void main(){
func(9, 20, 40, 30);
system("pause");
}
*/
//類 C++ 類的普遍寫法
//#include"MyTeacher.h"
//void main(){
// MyTeacher t;
// t.setAge(23);
// t.setName("chengyuelxl");
// t.print();
// system("pause");
//}\
////構(gòu)造函數(shù) 析構(gòu)函數(shù) 拷貝構(gòu)造函數(shù)
//class MyClass{
//private:
// char* name;
// int age;
//public:
// //無(wú)參構(gòu)造函數(shù) 寫了就會(huì)覆蓋默認(rèn)的構(gòu)造函數(shù)
// MyClass(){
// cout << "無(wú)參構(gòu)造函數(shù)s" << endl;
// }
// MyClass(char* name,int age){
// cout << "有參構(gòu)造函數(shù)" << endl;
// this->name = name;
// this->age = age;
// }
// //析構(gòu)函數(shù)
//};
//void main(){
// MyClass c;
// MyClass cw("chengyue",16);
// system("pause");
//}
//構(gòu)造函數(shù) 析構(gòu)函數(shù) 拷貝構(gòu)造函數(shù)
//class MyClass{
//private:
// char* name;
// int age;
//public:
// MyClass(){
// this->name =(char)malloc(100);
// strcpy(name, "wer");
// age = 26;
// }
// //析構(gòu)函數(shù)
// //當(dāng)前對(duì)象被系統(tǒng)釋放時(shí)芦鳍,析構(gòu)函數(shù)被執(zhí)行
// //作用:善后處理
// ~MyClass(){
// cout << "析構(gòu)函數(shù)s" << endl;
// free(this->name);
// }
//};
//void func(){
// MyClass m1;
//}
//void main(){
// func();
//
// system("pause");
//}
//拷貝構(gòu)造函數(shù) 淺拷貝
//class MyClass{
//private:
// char name;
// int age;
//public:
// MyClass(char* name, int age){
// cout << "有參構(gòu)造函數(shù)" << endl;
// this->name = name;
// this->age = age;
// }
// //拷貝構(gòu)造函數(shù) (值拷貝)
// //默認(rèn)的拷貝構(gòu)造函數(shù)就是值拷貝
// MyClass(const MyClass &obj){
// this->name = obj.name;
// this->age = obj.age;
// cout << "拷貝構(gòu)造函數(shù)" << endl;
// }
// void myprint(){
// cout << name << "," << age << endl;
// }
//};
//void main(){
// MyClass my("chengyue", 24);
// MyClass m2 = my;
// m2.myprint();
// 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 << "有參構(gòu)造函數(shù)" << endl;
}
~Teacher(){
cout << "析構(gòu)" << endl;
//釋放內(nèi)存
free(this->name);
}
//深拷貝
Teacher(const Teacher &obj){
//復(fù)制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");
}
*/
//拷貝構(gòu)造函數(shù)被調(diào)用的場(chǎng)景
//1.聲明時(shí)賦值
//Teacher t2 = t1;
//t2.myprint();
//2.作為參數(shù)傳入,實(shí)參給形參賦值
//func1(t1);
//3.作為函數(shù)返回值返回葛账,給變量初始化賦值
//Teacher t3 = func1(t1);
//這里不會(huì)被調(diào)用
//Teacher t1 ;
//Teacher t2;
//t1 = t2;
//構(gòu)造函數(shù)中屬性對(duì)象的初始化列表 構(gòu)造函數(shù)后:
//class Teacher
//{
//private:
// char* name;
//
//public:
// Teacher(char* name){
// this->name = name;
// cout << "Teacher 有參構(gòu)造函數(shù)" << endl;
// };
// ~Teacher(){
// cout << "Teacher 析構(gòu)函數(shù)" << endl;
// }
// char* getName(){
// return this->name;
// }
//};
//class Student
//{
//private:
// int id;
// //屬性對(duì)象
// //Teacher t1=Teacher("miss cang");
// Teacher t1;
// Teacher t2;
// Teacher t3;
//public:
// Student(int id, char *t1_name, char *t2_name, char *t3_name)
// :t1(t1_name), t2(t2_name), t3(t3_name){
// this->id = id;
// cout << "Student 有參構(gòu)造函數(shù)" << endl;
// };
// void setId(int id){
// this->id = id;
// }
// void myprint(){
// cout << t1.getName() << t2.getName() << t3.getName() << endl;
// }
// ~Student(){
// cout << "Student 析構(gòu)函數(shù)" << endl;
// }
//};
//void func(){
// Student s1(13, "chang", "chan23424g", "chwerwrwange");
// //s1.setId(19);
// s1.myprint();
//}
//void main(){
// func();
// system("pause");
//}
// c++ new (delete) 內(nèi)存分配
// c malloc free
//class Teacher
//{
//private:
// char* name;
//public:
// Teacher(char* name){
// this->name = name;
// cout << "Teacher 有參構(gòu)造函數(shù)" << endl;
// };
// ~Teacher(){
// cout << "Teacher 析構(gòu)函數(shù)" << endl;
// }
// char* getName(){
// return this->name;
// }
// void setName(char* name){
// this->name = name;
// }
//};
//void func(){
//C++ h會(huì)調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)
/Teacher t2 = new Teacher("cjhemg");
cout << t2->getName << endl;
delete(t2);/
//C 不會(huì)調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)
//Teacher t3 = (Teacher)malloc(sizeof(Teacher));
//t3->setName("werwer");
//free(t3);
//}
//void main(){
// func();
//數(shù)組類型
/int* p1 = (int)malloc(sizeof(int) * 10);
p1[0] = 9;
free(p1);/
//int *p2 = new int[10];
//p2[0] = 19;
////釋放數(shù)組[]
//delete[] p2;
// system("pause");
//}
//靜態(tài)的屬性和函數(shù)賦值
//class Teacher
//{
//public:
// char* name;
// static int total;
//public:
// Teacher(char* name,int total){
// this->name = name;
// this->total = total;
// cout << "Teacher 有參構(gòu)造函數(shù)" << endl;
// };
// ~Teacher(){
// cout << "Teacher 析構(gòu)函數(shù)" << endl;
// }
// char* getName(){
// return this->name;
// }
// void setName(char* name){
// this->name = name;
// }
// //計(jì)數(shù)的靜態(tài)函數(shù)
// static void count(){
// total++;
// cout << total << endl;
// }
//};
////全局的地方對(duì)靜態(tài)屬性初始化賦值
//int Teacher::total = 9;
//void main(){
// cout << Teacher::total << endl;
// Teacher::total++;
// cout << Teacher::total << endl;
// Teacher::count();
// system("pause");
//}
//類的大小
//class A{
//public :
// int i;
// int j;
// int K;
// static int m;
//};
//class B{
//public:
// int i;
// int j;
// int K;
// char * nba;
// /void myorintf(){
// cout << "打印" << endl;
// };/
//};
//void main(){
// cout << sizeof(A) << endl;
// cout << sizeof(B) << endl;
// //C C++ 內(nèi)存分區(qū): 棧 堆 全局 (靜態(tài)成員柠衅,全局) 常量區(qū)(字符串),程序代碼區(qū)
// jvm stack , 堆, (通過(guò)對(duì)象的引用來(lái)操作堆內(nèi)存中的對(duì)象)籍琳,
// native method stack(本地方法棧 jni 中native方法)菲宴,
// 方法區(qū) (程序代碼)贷祈,程序計(jì)數(shù)器
// system("pause");
//}
// this 指針 函數(shù)是共享的 必須要有標(biāo)識(shí)當(dāng)前對(duì)象是誰(shuí)的辦法
// this 當(dāng)前對(duì)象的指針
//class Teacher
//{
//private:
// char* name;
// int age;
//public:
// Teacher(char* name, int age){
// this->name = name;
// this->age = age;
// cout << "Teacher 有參構(gòu)造函數(shù)" << endl;
// };
// ~Teacher(){
// cout << "Teacher 析構(gòu)函數(shù)" << endl;
// }
// //常函數(shù) 修飾的是 this
// //既不能修改指針的值 也不能修改指針指向的內(nèi)容
// void print() const{
// printf("%#x\n", this);
// //this->name = "werw";
// //改變this 指針的值 也不能改
// cout << this->name << "," << this->age << endl;
// }
//};
//void main(){
//
// Teacher t1("jack", 14);
// printf("%#x\n", &t1);
// const Teacher t2("tom", 14);//常量對(duì)象只能調(diào)用常量函數(shù),
// //常函數(shù) 方式數(shù)據(jù)成員被非法訪問(wèn) 不能修改
//
// t1.print();
// t2.print();
//
// system("pause");
//}
//友元函數(shù)
//class A{
//private:
// int i;
//public:
// A(int i){
// this->i = i;
// }
// void myprint(){
// cout << i << endl;
// }
// //友元函數(shù)
// friend void modify_i(A* p,int a);
//};
////友元函數(shù)的實(shí)現(xiàn),在友元函數(shù)中可以訪問(wèn)私有的屬性
//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{
// //友元類 B可以訪問(wèn)A類中所有的私有成員
// friend class B;
//private:
// int i;
//public:
// A(int i){
// this->i = i;
// }
// void myprint(){
// cout << i << endl;
// }
//};
//class B{
//public:
// //友元類可以訪問(wèn)A類中的所有的成員 class.setAccessable(true)
// void accessAny(){
// a.i=30;
// }
//private:
// A a;
//};
//運(yùn)算符重載
//class Point{
//public:
// int x;
// int y;
//public:
// Point(int x,int y){
// this->x = x;
// this->y = y;
// }
// void myprint(){
// cout << x << "," << y << endl;
// }
// // 重載+ 號(hào)
// Point operator+( Point &p2){
// return Point(this->x + p2.x, this->y + p2.y);
// };
// // 重載- 號(hào)
// Point operator-( Point &p2){
// return Point(this->x - p2.x, this->y - p2.y);
// };
//};
//// 重載+ 號(hào)
//Point operator+(Point &p1, Point &p2){
// return Point(p1.x + p2.x, p1.y + p2.y);
//};
//
//// 重載- 號(hào)
//Point operator-(Point &p1, Point &p2){
// return Point(p1.x - p2.x, p1.y - p2.y);
//};
//運(yùn)算符的重載還是 函數(shù)的調(diào)用
//void main(){
// Point p1(12,23);
// Point p2(124,23);
// Point p3 = p1 + p2;
// p3.myprint();
// Point p4 = p1 - p2;
// p4.myprint();
// system("pause");
//}
//當(dāng)屬性私有時(shí)喝峦,通過(guò)友元函數(shù)完成函數(shù)的重載
class Point{
friend Point operator+(Point &p1, Point &p2);
friend Point operator-(Point &p1, Point &p2);
private:
int x;
int y;
public:
Point(int x, int y){
this->x = x;
this->y = y;
}
void myprint(){
cout << x << "," << y << endl;
}
};
// 重載+ 號(hào)
Point operator+(Point &p1, Point &p2){
return Point(p1.x + p2.x, p1.y + p2.y);
};
// 重載- 號(hào)
Point operator-(Point &p1, Point &p2){
return Point(p1.x - p2.x, p1.y - p2.y);
};
void main(){
Point p1(12, 23);
Point p2(124, 23);
Point p3 = p1 + p2;
Point p4 = p1 - p2;
p3.myprint();
p4.myprint();
system("pause");
}