//#include <iostream>
//using namespace std;
//
//class Test1
//{
//private:
// int a;
//public:
// Test1(int m_a)
// {
// a = m_a;
// cout << "调用test1构造函数" << endl;
// }
// ~Test1()
// {
// cout << "调用test1析构函数" << endl;
// }
//
//};
//
//class Test2
//{
//private:
// int b;
//public:
// Test2(int m_b)
// {
// b = m_b;
// cout << "调用test2构造函数" << endl;
// }
// ~Test2()
// {
// cout << "调用test2析构函数" << endl;
// }
//};
//
//class Test
//{
//private:
// int c;
// Test1 t1;
// Test2 t2; //对象成员 /。。组合
// const int m;//const成员变量不能进行初始化,定义只是一个声明,变量时不存在,不能赋值
//public:
// Test(int m_a,int m_b,int m_c):t1(m_a),t2(m_b),m(m_a)
// {
// c = m_c;
// cout << "调用Test构造函数" << endl;
// }
// ~Test()
// {
// cout << "调用Test析构函数" << endl;
// }
// int get_m()
// {
// return m;
// }
//
//};
//
//void fun()
//{
// Test t(1,2,3);
// cout << t.get_m() << endl;
//}
//
//int main()
//{
// fun();
//
// system("pause");
// return 0;
//#include <iostream>
//using namespace std;
//
//class Test
//{
//private:
// int a;
//public:
// Test(int m_a)
// {
// a = m_a;
// cout << "构造函数被调用。。。" << endl;
// }
// ~Test()
// {
// cout << "析构函数被调用。。。" << endl;
// }
//
//};
//
//void fun()
//{
// //Test t(10);
// cout << "----------" << endl;
// Test(10); //他的生命周期只在这一行。(匿名对象)
// cout << "----------" << endl;
//
//}
//int main()
//{
// fun();
// system("pause");
// return 0;
//}
///*
//*this指针 :this 指针是一个 指向当前 被操作的对象的特殊指针
//*/
//#include <iostream>
//using namespace std;
//
//class Test
//{
//private:
// int a;
// int b;
// int c;
//public:
// Test(int a,int b,int c)
// {
// this->a = a;
// //a = m_a;
// this->b = b;
// this->c = c;
// cout << "构造函数Test(int m_a,int m_b,int m_c)被调用。。。" << endl;
// }
// Test(int m_a,int m_b)
// {
// a = m_a;
// b = m_b;
//
// Test(m_a,m_b,30);//他的生命周期只在这一行。(匿名对象)
//
// cout << "构造函数Test(int m_a,int m_b)被调用。。。" << endl;
// }
// ~Test()
// {
// cout << "析构函数被调用。。。" << endl;
// }
//
// int get_c()
// {
// return c;
// }
//
//};
//
//void fun()
//{
// Test t1(10,20);
// cout << t1.get_c() << endl;
//}
//int main()
//{
// fun();
// system("pause");
// return 0;
//}
/*#include <iostream>
using namespace std;
int main()
{
//malloc free
int *pa = (int *)malloc(sizeof(int)/sizeof(char));
*pa = 20;
cout << "*pa=" << *pa << endl;
free(pa);
pa = NULL;
//C++中为了与C兼容 仍然保留了malloc free(函数) 不建议用
//C++提供了一个更加便捷的方法 叫new 和 delete (运算符!!!)
int *pb = new int(20);
float *p = new float(4.13);
cout << "*pb=" << *pb << endl;
delete pb; //基础类型
//分配数组
int *p = (int*)malloc(sizeof(int)*10);
free(p);
int *p2 = new int[10]; //new分配数组的时候 new = 类型[];///用new分配数组的时候不可以进行初始化!!!!
int *p3 = new int[2][2];
for(int i = 0;i < 10;i++)
{
p2[i] = i;
}
for(int j = 0;j < 10;j++)
{
cout << p2[j] << "," ;
}
//delete 释放数组 delete[] 指针;
// 如果没有加[] 会造成内存泄露
delete[] p2;
system("pause");
return 0;
}*/
///*
//malloc free 和 new delete 区别:
// malloc 和 free 是由c语言标准库提供的一个 库函数;
// new 和 delete 是运算符 (作为运算符 是有重载机制的)
//
//有了malloc 为何还要用new:
// 对于非内部数据类型的对象而言,对象需要在消亡之前自动执行析构函数。
// 由于 malloc 是库函数而不是运算符,不在编译器的控制权限之内,不能将执行构造函数和析构函数的任务
// 加在malloc/free 因此。。。。
//*/
//
//#include <iostream>
//using namespace std;
//
//class Test
//{
//private:
// int a;
// int b;
//public:
// /*Test()
// {
// cout << "无参构造函数被调用" << endl;
// }*/
// Test(int a)
// {
// this->a = a;
// cout << "有参构造函数被调用 。。。" << endl;
// }
// Test(int a,int b)
// {
// this->a = a;
// this->b = b;
// cout << "有参(2)构造函数被调用 。。。" << endl;
// }
// ~Test()
// {
// cout << "析构函数被调用。。。" << endl;
// }
//};
//
//void fun()
//{
// //Test t1(10);
// //能不能用 malloc 来分配对象呢?
// /*Test *t = (Test*)malloc(sizeof(Test)/sizeof(char));
// if(t == NULL)
// {
// cout << "分配不成功!" << endl;
// }
// free(t);*/
// cout << "-----------" << endl;
// Test *t = new Test; //创建对象时 调用构造函数
// cout << "---------" << endl;
// delete t; //delete 释放空间(销毁) 调用析构函数
// cout << "-----------" << endl;
//
// Test *t2 = new Test(20,30); //在new一个对象的时候可以直接对这个对象初始化 并且调用相关的构造函数
// delete t2;
//}
//
//int main()
//{
// fun();
// system("pause");
// return 0;
//}
///*
//静态? static????
//void fun()
//{
// static int a = 0;
// a ++;
// cout << a << endl;
//}
//静态成员:无论创建了多少个对象 这些对象都 共享 这个static成员
//静态成员变量 :
// 1. 静态成员变量需要在类外初始化!! 类型 类名::变量名 = 初始值;
// 2. 静态成员变量访问权限依然存在 类名:变量名 (public)
//*/
//#include <iostream>
//using namespace std;
//
void fun()
{
static int a = 0;
a++;
cout << a << endl;
}
//
//class Test
//{
//private:
// int a;
// static int b;
//public:
// static int c;
// Test(int x,int y,int z)
// {
// a = x;
// b = y;
// c = z;
// cout << "test构造函数被调用了" << endl;
// }
// ~Test()
// {
// cout << "test析构函数被调用" << endl;
// }
//};
//int Test::b = 0;
//int Test::c = 0;
//
//void fun()
//{
// Test t(10,20,30);
// cout << "C:" << Test::c << endl;
// //cout << "A:" << Test::b << endl;
//
//}
//
//int main()
//{
// fun();
//
// system("pause");
// return 0;
///*
//静态? static????
//void fun()
//{
// static int a = 0;
// a ++;
// cout << a << endl;
//}
//静态成员:无论创建了多少个对象 这些对象都 共享 这个static成员,不含有隐藏的*this指针
// 静态成员变量 :
// 1. 静态成员变量需要在类外初始化!! 类型 类名::变量名 = 初始值;
// 2. 静态成员变量访问权限依然存在 类名::变量名 (public)
// 静态成员函数:
// 1. 形式? static void fun(){ ... }
// 2. 在静态成员函数中只能访问所在类的静态成员变量,绝对不可以使用其他普通的成员变量
// (全局变量 常量 外部函数)
// 3. 访问? 类名::静态成员函数(<实参表>) Test::fun();
// 如果在静态成员函数中非要访问普通成员变量 ? 可以
// class A{
// int x;
// static int y;
//
// static void show1(){.....y ....;}
// static void show2(A a){cout << a.x << endl;} // 非要访问这个X,只能通过对象访问类的非静态成员
//*/
//
//#include <iostream>
//using namespace std;
//
//void num_show();
//class Computer
//{
//private:
// float price;
// static float total;
//public:
// static int num;
// Computer(float p)
// {
// price = p;
// total += p;
// num ++;
// }
// ~Computer(){}
// void show()
// {
// cout << "the computer price is:" << price << endl;
// }
// static void t_show()
// {
// num_show();
// cout << "total price is :" << total << endl;
// }
//
//
//};
//
//void num_show()
//{
// cout << "total number is:" << Computer::num << endl;
//}
//
//float Computer::total = 0;
//int Computer::num = 0;
//
//int main()
//{
// Computer::t_show();
// Computer c1(4000);
// c1.show();
// Computer::t_show();
// Computer c2(7000);
// c2.show();
// Computer::t_show();
//
// system("pause");
// return 0;
///*
//友元: 友元函数/友元成员函数/友元类
//友元?友元提供了(在不同类的成员函数之间,类的成员函数与一般函数之间进行的)共享数据的机制
// 通过友元函数 一个普通函数或者其他类的成员函数可以访问类中的private成员。
//1. 友元函数
// 和一个外部的 普通函数 交朋友!
// 建立关系 : friend int get_area(CBox &b)
//
//*/
//
//#include<iostream>
//using namespace std;
//
//class CBox
//{
//private:
// int i;
// int j;
//public:
// CBox(int a=10,int b=10)
// {
// i = a;
// j = b;
// }
// friend int get_area(CBox &b);
// friend int get_area2();
//};
//
//int get_area(CBox &b)
//{
// return b.i * b.j;
//}
//int get_area2()
//{
// CBox c1;
// return c1.i * c1.j;
//}
//int main()
//{
// CBox c(10,20);
// cout << "area is :" << get_area2() << endl;
//
// system("pause");
// return 0;
//}
///*
//友元: 友元函数/友元成员函数/友元类
//友元?友元提供了(在不同类的成员函数之间,类的成员函数与一般函数之间进行的)共享数据的机制
// 通过友元函数 一个普通函数或者其他类的成员函数可以访问类中的private成员。
//1. 友元函数
// 和一个外部的 普通函数 交朋友!
// 建立关系 : friend int get_area(CBox &b)
//2. 友元成员函数
// 成员函数属于谁? 我(需要访问)
// 朋友是谁? 你需要某个人的数据 他就是朋友
// friend声明在哪? 谁声明friend谁就要把自己的东西分享出去
// 分享东西出去的那个人那里声明friend
// 注意
// 由于A2的定义在A1之后 对A2进行引用性声明
// 在friend声明由于是在A1的类外 所以需要 friend void A1::change(A2 &a1);
//*/
//
//#include<iostream>
//using namespace std;
//class A2;
//class A1
//{
//private:
// int a;
// int b;
//public:
// A1(int i,int j){a = i;b = j;}
// int Geta(){return a;}
// int Getb(){return b;}
// void change(A2 &a1);
//};
类外定义 返回值 类名::函数名 ( )
//class A2
//{
//private:
// int c;
// int d;
//public:
// A2(int i,int j){c = i;d = j;}
// int Geta(){return c;}
// int Getb(){return d;}
// friend void A1::change(A2 &a1); //谁声明friend谁就要把自己的东西分享出去
//};
//
//void A1::change(A2 &a1)
//{
// a = a1.c;
// b = a1.d;
//}
//int main()
//{
// A1 a1(2,3);
// A2 a2(4,5);
// cout << "1 a:" << a1.Geta() << ",b :" << a1.Getb() << endl;
// cout << "2 a:" << a2.Geta() << ",b :" << a2.Getb() << endl;
// a1.change(a2);
// cout << "1 a:" << a1.Geta() << ",b :" << a1.Getb() << endl;
//
// system("pause");
// return 0;
//}
///*
//友元: 友元函数/友元成员函数/友元类
//友元?友元提供了(在不同类的成员函数之间,类的成员函数与一般函数之间进行的)共享数据的机制
// 通过友元函数 一个普通函数或者其他类的成员函数可以访问类中的private成员。
//1. 友元函数
// 和一个外部的 普通函数 交朋友!
// 建立关系 : friend int get_area(CBox &b)
//2. 友元成员函数
// 成员函数属于谁? 我(需要访问)
// 朋友是谁? 你需要某个人的数据 他就是朋友
// friend声明在哪? 谁声明friend谁就要把自己的东西分享出去
// 分享东西出去的那个人那里声明friend
// 注意
// 由于A2的定义在A1之后 对A2进行引用性声明
// 在friend声明由于是在A1的类外 所以需要 friend void A1::change(A2 &a1);
//3. 友元类
// 注:1.在A2中可以访问A1里的成员! 反之不行!
// 2.友元类关系中 没有传递性!(A是B的友元 B又是C的友元 A和C也是友元?错误!!!!)
//注:友元!破坏了封装性 因此谨慎使用友元关系
// 如何正确使用? 用友元的时候 一般用来获取 不修改
//*/
//
//#include<iostream>
//using namespace std;
//
//class A1
//{
// ....//成员变量
// ....//成员函数
// friend class A2; //谁声明friend谁就要把自己的东西分享出去
//
//};
//class A2
//{
// ....//成员变量
// ....//成员函数
//
//};
//
//
//int main()
//{
//
//
//
//}
///*
//const 常量修饰符 不可以被修改
//1. const 对象 -- 不可以改变const对象中的成员数据!!!!
// 一般格式: 类名 const 对象名 A const a1(1,2);
// const 类名 对象名 const A a1(4,5);
//
//
//*/
//#include<iostream>
//using namespace std;
//
//class A
//{
//private:
// int a;
//public:
// int b;
// A(int i,int j)
// {
// a = i;
// b = j;
// }
// void set(int i,int j)
// {
// a = i;
// b = j;
// }
// void show()
// {
// cout << "a;" << a << ",b;" << endl;
// }
//};
//
//int main()
//{
// A const a1(10,20);
// const A a2(30,40);
//
// //a1.b = 100;
// //a1.set(1,2); 任何修改const对象的成员变量的行为都是非法的
//
// system("pause");
// return 0;
//}
/*
const 常量修饰符 不可以被修改
1. const 对象 -- 不可以改变const对象中的成员数据!!!!
一般格式: 类名 const 对象名 A const a1(1,2);
const 类名 对象名 const A a1(4,5);
2. const 成员函数
一般const与成员函数相结合
①将const放在成员函数名的前面 const int func()
表示返回值为一个常量
②const成员函数:是成员函数特有的 将const放在成员函数的参数表之后
void show() const;
外部 void Test::show() const //const 不可以省略
{ ... }
③const成员函数 不可以改变成员变量的值 不能调用类中非const成员函数!!!!!!!
const对象只能调用const成员函数
普通对象优先调用非const成员函数,假如没有 那就只能调用同名的const成员函数
3. const 成员变量(初始化列表)
由于const成员数据不能在定义类时直接赋值,所以要用 初始化成员列表。。。
总结归纳:
① 声明为const对象 所有成员变量不可被修改! 在声明const对象时候给他初始化
② 声明为const对象 则该const对象 只能调用他的const成员函数
③ const成员函数不能改变对象的成员数据,也不可以调用类中非const成员函数
④ 对于const 成员变量(初始化列表)
*/
#include<iostream>
using namespace std;
class Sample
{
private:
int a;
int b;
public:
Sample(int x,int y)
{
a = x;
b = y;
}
~Sample(){}
//void print();
void print() const;
void test(){cout << "---" << endl;}
};
//void Sample::print()
//{
// cout << "调用void Sample::print()" << endl;
//}
void Sample::print() const
{
cout << "调用void Sample::print() const" << endl;
//a = 2; 不可以改变成员变量的值
//test(); 不能调用类中非const成员函数
}
int main()
{
Sample a1(1,2);
const Sample s2(3,4);
s2.print(); //onst对象只能调用const成员函数
a1.print(); //普通对象优先调用非const成员函数,假如没有 那就只能调用同名的const成员函数
system("pause");
return 0;
}