先给对象开辟空间(实例化) 然后调用构造函数(初始化)
class Data{public :int mA ;public :// 无参构造函数Data (){mA = 0 ;cout << " 无参构造函数 " << endl ;}// 有参构造函数Data ( int a ){mA = a ;cout << " 有参构造函数 mA=" << mA << endl ;}};int main (){// 隐式调用无参构造函数(推荐)Data ob1 ;// 显示调用无参构造函数Data ob2 = Data ();// 隐式调用有参构造函数(推荐)Data ob3 ( 10 );// 显示调用有参构造函数Data ob4 = Data ( 10 );// 匿名对象 ( 无参 ) 当前语句技术 立即释放Data ();Data ( 20 );// 构造函数隐式转换(类中只有一个数据成员)Data ob5 = 100 ;}
class Data1{public :int mA ;public :// 无参构造函数Data1 (){mA = 0 ;cout << " 无参构造函数 " << endl ;}// 有参构造函数Data1 ( int a ){mA = a ;cout << " 有参构造函数 mA=" << mA << endl ;}// 析构函数~Data1 (){cout << " 析构函数 mA=" << mA << endl ;}};
#include class Data2{public :char * name ;public :Data2 (){name = NULL ;}Data2 ( char * str ){name = new char [ strlen ( str ) + 1 ];strcpy ( name , str );cout << " 有参构造 " << endl ;}~Data2 (){if ( name != NULL )delete [] name ;cout << " 析构函数 " << endl ;}};int main ( int argc , char * argv []){Data2 ob ( "hello world" );cout << ob . name << endl ;return 0 ;}
#include using namespace std ;class Data{public :int mA ;public :Data (){cout << " 无参构造 " << endl ;}Data ( int a ){mA = a ;cout << " 有参构造 mA=" << mA << endl ;}#if 1// 拷贝构造的定义形式 :ob 就是旧对象的引用Data ( const Data & ob ){// 一旦实现了 拷贝构造函数 必须完成赋值操作mA = ob . mA ;cout << " 拷贝构造函数 " << endl ;}#endif~Data (){cout << " 析构函数 mA=" << mA << endl ;}};int main ( int argc , char * argv []){Data ob1 ( 10 );// 旧对象给新对象初始化 就会调用拷贝构造函数Data ob2 = ob1 ;cout << "ob2.mA =" << ob2 . mA << endl ;return 0 ;}
Data ob1 ( 10 );Data ob2 = ob1 ; // 调用拷贝构造
Data ob1 ( 10 );Data & ob2 = ob1 ; // 不会调用拷贝构造
void func ( Data ob ) //Data ob=ob1{}int main (){Data ob1 ( 100 ); // 有参构造func ( ob1 ); // 拷贝构造}
#include #include using namespace std ;class Data5{public :char* name ;public :Data5 (){name = NULL ;}Data5 ( char* str ){name = new char [ strlen ( str ) + 1 ];strcpy ( name , str );cout << " 有参构造 name=" << name << endl ;}Data5 ( const Data5 & ob ) // 深拷贝{// 为对象的指针成员申请独立的空间name = new char [ strlen ( ob . name ) + 1 ];strcpy ( name , ob . name );cout << " 拷贝构造函数 " << endl ;}~Data5 (){cout << " 析构函数 name = " << name << endl ;if ( name != NULL ){delete [] name ;name = NULL ;}}};void test05 (){Data5 ob1 (( char * ) "hello world\n" );Data5 ob2 = ob1 ;}
于是解决这个问题,就需要深拷贝构造。
#include
using namespace std;
class A
{
public:
int mA;
public:
A()
{
mA = 0;
cout << "A的无参构造" << endl;
}
A(int a)
{
mA = a;
cout << "A的有参构造" << endl;
}
~A()
{
cout << "A的析构函数" << endl;
}
};
class B
{
public:
int mB;
A ob;//成员对象
public:
B()
{
cout << "B类的无参构造" << endl;
}
//初始化列表 成员对象 必须使用对象名+() 重要
B(int a, int b) : ob(a)
{
mB = b;
cout << "B类的有参构造" << endl;
}
~B()
{
cout << "B的析构函数" << endl;
}
};
int main(int argc, char* argv[])
{
B ob1(10, 20);
cout << "mA =" << ob1.ob.mA << ", mB =" << ob1.mB << endl;
return 0;
}
#include
using namespace std;
class MyString {
public:
explicit MyString(int n) {
cout << "MyString(int n)!" << endl;
}
MyString(const char* str) {
cout << "MyString(const char* str)" << endl;
}
};
int main() {
//给字符串赋值?还是初始化?
//MyString str1 = 1;
MyString str2(10);
//寓意非常明确,给字符串赋值
MyString str3 = "abcd";
MyString str4("abcd");
return 0;
}
MyString str1 = 1; 隐式转换,看的是后面1的类型!寻找1的类型为整型,就会去找MyString(int n)来执行,但是会发现这种书写容易让人造成歧义,因此加上explicit MyString(int n)这种写法就禁止让程序员出现MyString str1 = 1;这种写法。
而下面的MyString str3 = "abcd";寓意比较明确就没加!
#include
using namespace std;
class A
{
public:
int mA;
public:
A()
{
mA = 0;
cout << "A的无参构造 mA=" << mA << endl;
}
A(int a)
{
mA = a;
cout << "A的有参构造mA=" << mA << endl;
}
~A()
{
cout << "A的析构函数 mA = " << mA << endl;
}
};
int main()
{
//对象数组 每个元素都会自动调用构造和析构函数
//对象数组不初始化 每个元素 调用无参构造
A arr1[5];
//对象数组的初始化 必须显示使用有参构造 逐个元素初始化
A arr2[5] = { A(10),A(20),A(30),A(40),A(50) };
int n = sizeof(arr2) / sizeof(arr2[0]);
int i = 0;
for (i = 0; i < n; i++)
{
cout << arr2[i].mA << " ";
}
cout << endl;
}
这里析构会先析构 A arr2[5] = { A(10),A(20),A(30),A(40),A(50) };,其次才是 A arr1[5];,因为两个数组是同级别的,栈先进后出,然后 A arr2[5] = { A(10),A(20),A(30),A(40),A(50) };这里面的元素也是同级别的,于是显示先进后出!
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Person {
public:
Person() {
mAge = 20;
pName = (char*)malloc(strlen("john") + 1);
strcpy(pName, "john");
}
void Init() {
mAge = 20;
pName = (char*)malloc(strlen("john") + 1);
strcpy(pName, "john");
}
void Clean() {
if (pName != NULL) {
free(pName);
}
}
public:
int mAge;
char* pName;
};
int main() {
//分配内存
Person* person = (Person*)malloc(sizeof(Person));
if (person == NULL) {
return 0;
}
//调用初始化函数
person->Init();
//清理对象
person->Clean();
//释放person对象
free(person);
return 0;
}
1) 程序员必须确定对象的长度。 (sizeof(Person)2) malloc 返回一个 void 指针, c++ 不允许将 void赋值给其他任何指针,必须强转。 (Person*)3) malloc可能申请内存失败,所以必须判断返回值来确保内存分配成功。if (person == NULL) {
return 0;
}4) 用户在使用对象之前必须记住对他初始化,构造函数不能显示调用初始化 ( 构造函数是由编译器调用 ) ,用 户有可能忘记调用初始化函数。//调用初始化函数
person->Init();
//清理对象
person->Clean();
//释放person对象
free(person);
Person* person = new Person;
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Person {
public:
Person() {
cout << "无参构造函数!" << endl;
pName = new char[strlen("undefined") + 1];
strcpy(pName, "undefined");
mAge = 0;
}
Person(char* name, int age) {
cout << "有参构造函数!" << endl;
pName = new char[strlen(name) + 1];
strcpy(pName, name);
mAge = age;
}
void ShowPerson() {
cout << "Name:" << pName << " Age:" << mAge << endl;
}
~Person() {
cout << "析构函数!" << endl;
if (pName != NULL) {
delete[] pName;
pName = NULL;
}
}
public:
char* pName;
int mAge;
};
int main() {
Person* person1 = new Person;//会调用无参构造
Person* person2 = new Person("John", 33);//会调用有参构造
person1->ShowPerson();
person2->ShowPerson();
delete person1;
delete person2;
}
class Person {
public:
Person() {
pName = NULL;
mAge = 0;
}
Person(char* name, int age) {
pName = new char[strlen(name) + 1];
strcpy(pName, name);
mAge = age;
}
~Person() {
if (pName != NULL) {
delete[] pName;
}
}
public:
char* pName;
int mAge;
};
void test() {
//栈聚合初始化
Person person[] = { Person("john", 20), Person("Smith", 22) };
cout << person[1].pName << endl;
//创建堆上对象数组必须提供构造函数
Person* workers = new Person[20];
delete[] workers;
}
Person* workers = new Person[20];
delete[] workers;因为new的时候有[],所以delete也要有!
class Data
{
public:
int a;//普通成员数据
//类中定义
static int b;//静态成员数据
};
//类外初始化
int Data::b = 100;//不用加static
void test01()
{
//静态成员数据 通过类名称直接访问(属于类)
cout << Data::b << endl;
//静态成员数据 通过对象访问(共享)
Data ob1;
cout << ob1.b << endl;//100
ob1.b = 200;
Data ob2;
ob2.b = 300;
cout << Data::b << endl;//300
}
class Data2
{
public:
int mA;
static int count;
public:
Data2()
{
count++;
}
Data2(int a)
{
mA = a;
count++;
}
Data2(const Data2& ob)
{
count++;
}
~Data2()
{
count--;
}
};
int Data2::count = 0;
void test02()
{
Data2 ob1;
Data2 ob2(10);
Data2 ob3 = ob2;
cout << "对象个数:" << Data2::count << endl;//3
{
Data2 ob4;
Data2 ob5;
cout << "对象个数:" << Data2::count << endl;//5
}
cout << "对象个数:" << Data2::count << endl;//3
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
#include
using namespace std;
class SingleTon//单例模式
{
//构造私有化 防止实例化其他对象
private:
SingleTon() {
count = 0;
cout << "构造" << endl;
}
SingleTon(const SingleTon & ob) {
count = 0;
}
~SingleTon()
{
cout << "析够" << endl;
}
private:
//const防止p 在类内部 被修改指向
static SingleTon* const p;//保存唯一的实例地址
int count;//统计任务执行次数
public:
static SingleTon* getSingleTon(void)//获取唯一的实例地址
{
return p;
}
//用户自定义 任务函数
void printString(char* str)
{
count++;
cout << "当前第" << count << "次任务打印:" << str << endl;
}
};
SingleTon* const SingleTon::p = new SingleTon;//创建唯一的实例
int main(int argc, char* argv[])
{
//获取单例的地址
SingleTon* p1 = SingleTon::getSingleTon();
p1->printString("离职证明1");
p1->printString("学历证明1");
p1->printString("学位证明1");
p1->printString("身份证明1");
SingleTon* p2 = SingleTon::getSingleTon();
p2->printString("离职证明2");
p2->printString("学历证明2");
p2->printString("学位证明2");
p2->printString("身份证明2");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class MyClass01 {
public:
int mA;
};
class MyClass02 {
public:
int mA;
static int mB;
};
class MyClass03 {
public:void printMyClass() {
cout << "hello world!" << endl;
}
public:
int mA;
static int mB;
};
class MyClass04 {
public:
void printMyClass() {
cout << "hello world!" << endl;
}
static void ShowMyClass() {
cout << "hello world!" << endl;
}
public:
int mA;
static int mB;
};
int main() {
MyClass01 mclass01;
MyClass02 mclass02;
MyClass03 mclass03;
MyClass04 mclass04;
cout << "MyClass01:" << sizeof(mclass01) << endl; //4
//静态数据成员并不保存在类对象中
cout << "MyClass02:" << sizeof(mclass02) << endl; //4
//非静态成员函数不保存在类对象中
cout << "MyClass03:" << sizeof(mclass03) << endl; //4
//静态成员函数也不保存在类对象中
cout << "MyClass04:" << sizeof(mclass04) << endl; //4
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Data
{
public:
int a;
int b;
mutable int c;
public:
Data(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
//const 修饰成员函数为只读(该成员函数不允许对 成员数据 赋值) mutable修饰的成员除外
void showData(void) const
{
//a = 100;//err
c = 100;
cout << a << " " << b << " " << c << endl;
}
};
int main()
{
Data ob1(10, 20, 30);
ob1.showData();
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Room
{
friend void visiting01(Room& room);
private:
string bedRoom;//卧室
public:
string setingRoom;//客厅
public:
Room(string bedRoom, string setingRoom)
{
this->bedRoom = bedRoom;
this->setingRoom = setingRoom;
}
};
//普通全局函数
void visiting01(Room& room)
{
cout << "访问了" << room.setingRoom << endl;
cout << "访问了" << room.bedRoom << endl;
}
int main(int argc, char* argv[])
{
Room room("卧室", "客厅");
visiting01(room);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Room;//向前声明 只能说明类名称
class goodGay
{
public:
void visiting01(Room& room);
void visiting02(Room& room);
}; class Room
{
friend void goodGay::visiting02(Room& room);
private:
string bedRoom;//卧室
public:
string setingRoom;//客厅
public:
Room(string bedRoom, string setingRoom)
{
this->bedRoom = bedRoom;
this->setingRoom = setingRoom;
}
};
void goodGay::visiting01(Room& room)
{
cout << "访问了" << room.setingRoom << endl;
//cout<<"访问了"<
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Room;//向前声明 只能说明类名称
class goodGay
{
public:
void visiting01(Room& room);
void visiting02(Room& room);
};
class Room
{
friend class goodGay;
private:
string bedRoom;//卧室
public:
string setingRoom;//客厅
public:
Room(string bedRoom, string setingRoom) {
this->bedRoom = bedRoom;
this->setingRoom = setingRoom;
}
};
void goodGay::visiting01(Room& room)
{
cout << "访问了" << room.setingRoom << endl;
cout << "访问了" << room.bedRoom << endl;
}
void goodGay::visiting02(Room& room)
{
cout << "好基友访问了" << room.setingRoom << endl;
cout << "好基友访问了" << room.bedRoom << endl;
}
int main(int argc, char* argv[])
{
Room room("卧室", "客厅");
goodGay ob;
ob.visiting01(room);
ob.visiting02(room);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
#include
using namespace std;
class TV;
//遥控器的类作为TV的友元
class Remote
{
private:
TV* p;
public:
Remote(TV* p);
void offOrOn(void);
void upVolume(void);
void downVolume(void);
void upChannel(void); void downChannel(void);
void showTv(void);
void setChannel(int channel);
};
class TV
{
friend class Remote;
enum { OFF, ON };
enum { minVol, maxVol = 10 };
enum { minChan, maxChan = 25 };
private:
int state;
int volume;
int channel;
public:
TV()
{
state = OFF;
volume = minVol;
channel = minChan;
}
void offOrOn(void);
void upVolume(void);
void downVolume(void);
void upChannel(void);
void downChannel(void);
void showTv(void);
};
int main(int argc, char* argv[])
{
TV tv;
Remote re(&tv);
re.offOrOn();
re.upVolume();
re.upVolume();
re.setChannel(10);
re.showTv();
return 0;
}
void TV::offOrOn()
{
state = (state == OFF ? ON : OFF);
}
void TV::upVolume()
{
if (volume == maxVol)
{
cout << "音量已经最大" << endl;
return;
}volume++;
}
void TV::downVolume()
{
if (volume == minVol)
{
cout << "音量已经最小" << endl;
return;
}
volume--;
}
void TV::upChannel()
{
if (channel == maxChan)
{
cout << "频道已经最大" << endl;
return;
}
channel++;
}
void TV::downChannel()
{
if (channel == minChan)
{
cout << "频道已经最小" << endl;
return;
}
channel--;
}
void TV::showTv()
{
cout << "当前电视机的状态:" << (state == OFF ? "关" : "开") << endl;
cout << "当前电视机的音量:" << volume << endl;
cout << "当前电视机的频道:" << channel << endl;
}
Remote::Remote(TV* p)
{
this->p = p;
}
void Remote::offOrOn()
{
p->offOrOn();
}
void Remote::upVolume()
{
p->upVolume();
}
void Remote::downVolume()
{
p->downVolume();
}
void Remote::upChannel()
{
p->upChannel();
}
void Remote::downChannel()
{
p->downChannel();
}
void Remote::showTv()
{
p->showTv();
}
void Remote::setChannel(int channel)
{
if (channel >= TV::minChan && channel <= TV::maxChan)
{
p->channel = channel;
}
else
{
cout << "频道" << channel << "不在有效范围内" << endl;
}
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
#include
using namespace std;
class Person
{
friend ostream& operator<<(ostream& out, Person& ob);
private:
int num;
string name;
float score;
public:
Person() {}
Person(int num, string name, float score) :num(num), name(name), score(score) {}
};
//全局函数重载operator <<
ostream & operator<<(ostream & out, Person & ob)
{
out << ob.num << " " << ob.name << " " << ob.score << endl;
return out;
}
int main(int argc, char* argv[])
{
Person lucy(100, "lucy", 99.8f);
Person bob(101, "bob", 99.8f);
cout << lucy << bob << endl;//operator<<(cout, lucy);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
#include
#include
using namespace std;
class Person
{
friend ostream& operator<<(ostream& out, Person& ob);
friend istream& operator>>(istream& in, Person& ob); private:
int num;
string name;
float score;
public:
Person() {}
Person(int num, string name, float score) :num(num), name(name), score(score) {}
};
//全局函数重载operator<<
ostream& operator<<(ostream& out, Person& ob)
{
out << ob.num << " " << ob.name << " " << ob.score << endl;
return out;
}
//全局函数重载operator>>
istream& operator>>(istream& in, Person& ob)
{
in >> ob.num >> ob.name >> ob.score;
return in;
}
int main(int argc, char* argv[])
{
Person lucy;
Person bob;
cin >> lucy >> bob;
cout << lucy << bob << endl;
return 0;
}