添加友元

This commit is contained in:
朱毅骏 2021-09-15 18:32:17 +08:00
parent 2256245596
commit 48971c53ac
4 changed files with 205 additions and 1 deletions

View File

@ -75,4 +75,7 @@ add_executable(error3 exception/error3.cpp)
add_executable(exc_mean exception/exc_mean.cpp)
add_executable(error5 exception/error5.cpp)
add_executable(newexcp exception/newexcp.cpp)
add_executable(use_sales exception/use_sales.cpp)
add_executable(use_sales exception/use_sales.cpp)
add_executable(rtti1 exception/rtti1.cpp)
add_executable(rtti2 exception/rtti2.cpp)
add_executable(constcast exception/constcast.cpp)

View File

@ -0,0 +1,27 @@
//
// Created by zhuyijun on 2021/9/15.
//
#include <iostream>
using namespace std;
void change(const int *pt, int n);
int main() {
int pop1 = 38383;
const int pop2 = 2000;
cout << "pop1,pop2: " << pop1 << ", " << pop2 << endl;
change(&pop1, -103);
change(&pop2, -103);
cout << "pop1,pop2: " << pop1 << ", " << pop2 << endl;
return 0;
}
void change(const int *pt, int n) {
int *pc;
pc = const_cast<int *>(pt);
*pc += n;
}

85
base/exception/rtti1.cpp Normal file
View File

@ -0,0 +1,85 @@
//
// Created by zhuyijun on 2021/9/14.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Grand {
private:
int hold;
public:
Grand(int h = 0) : hold(h) {}
virtual void Speak() const {
cout << "I am a grand class!\n";
}
virtual int Value() const {
return hold;
}
};
class Superb : public Grand {
public:
Superb(int h = 0) : Grand(h) {}
void Speak() const {
cout << "I am a superb class!!\n";
}
virtual void Say() const {
cout << "I hold the superb value of" << Value() << "!\n";
}
};
class Magnificent : public Superb {
private:
char ch;
public:
Magnificent(int h = 0, char c = 'A') : Superb(h), ch(c) {}
void Speak() const {
cout << "I am a magnificent class!!\n";
}
void Say() const {
cout << "I hold the character " << ch << " and the integer " << Value() << "!\n";
}
};
Grand *GetOne();
int main() {
srand(std::time(0));
Grand *pg;
Superb *ps;
for (int i = 0; i < 5; ++i) {
pg = GetOne();
pg->Speak();
if ((ps = dynamic_cast<Superb *>(pg))) {
ps->Say();
}
}
return 0;
}
Grand *GetOne() {
Grand *p;
switch (rand() % 3) {
case 0:
p = new Grand(rand() % 100);
break;
case 1:
p = new Superb(rand() % 100);
break;
case 2:
p = new Magnificent(rand() % 100, 'A' + rand() % 26);
break;
}
return p;
}

89
base/exception/rtti2.cpp Normal file
View File

@ -0,0 +1,89 @@
//
// Created by zhuyijun on 2021/9/14.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <typeinfo>
using namespace std;
class Grand {
private:
int hold;
public:
Grand(int h = 0) : hold(h) {}
virtual void Speak() const {
cout << "I am a grand class!\n";
}
virtual int Value() const {
return hold;
}
};
class Superb : public Grand {
public:
Superb(int h = 0) : Grand(h) {}
void Speak() const {
cout << "I am a superb class!!\n";
}
virtual void Say() const {
cout << "I hold the superb value of" << Value() << "!\n";
}
};
class Magnificent : public Superb {
private:
char ch;
public:
Magnificent(int h = 0, char c = 'A') : Superb(h), ch(c) {}
void Speak() const {
cout << "I am a magnificent class!!\n";
}
void Say() const {
cout << "I hold the character " << ch << " and the integer " << Value() << "!\n";
}
};
Grand *GetOne();
int main() {
srand(time(0));
Grand *pg;
Superb *ps;
for (int i = 0; i < 5; ++i) {
pg = GetOne();
cout <<"Now processing type "<< typeid(*pg).name() <<".\n";
pg->Speak();
if ((ps = dynamic_cast<Superb *>(pg))) {
ps->Say();
}
if (typeid(Magnificent) == typeid(*pg)){
cout <<"Yes, you're really magnificent.\n";
}
}
return 0;
}
Grand *GetOne() {
Grand *p;
switch (rand() % 3) {
case 0:
p = new Grand(rand() % 100);
break;
case 1:
p = new Superb(rand() % 100);
break;
case 2:
p = new Magnificent(rand() % 100, 'A' + rand() % 26);
break;
}
return p;
}