这里写自定义目录标题
欢迎使用Markdown编辑器
01th_oop
person.c
c
#include <stdio.h>
int main(int argc, char **argv)
{
char *zs_name = "zhangsan";
int zs_age = 10;
char *ls_name = "lisi";
int ls_age = 16;
printf("name = %s, age = %d\n", zs_name, zs_age);
printf("name = %s, age = %d\n", ls_name, ls_age);
return 0;
}
person2.c
c
#include <stdio.h>
int main(int argc, char **argv)
{
char *names[] = {"zhangsan", "lisi"};
char ages[] = {10, 16};
char *work[] = {"teacher", "doctor"};
int i;
for (i = 0; i < 2; i++)
{
printf("name = %s, age = %d\n", names[i], ages[i]);
}
return 0;
}
person3.c
c
#include <stdio.h>
struct person {
char *name;
int age;
char *work;
void (*printInfo)(struct person *per);
};
void printInfo(struct person *per)
{
printf("name = %s, age = %d, work = %s\n", per->name, persons[i].age, persons[i].work);
}
int main(int argc, char **argv)
{
struct person persons[] = {
{"zhangsan", 10, "teacher"},
{"lisi", 16, "doctor"},
};
int i;
for (i = 0; i < 2; i++)
{
printf("name = %s, age = %d, work = %s\n", persons[i].name, persons[i].age, persons[i].work);
}
return 0;
}
person4.c
c
#include <stdio.h>
struct person {
char *name;
int age;
char *work;
void (*printInfo)(struct person *per);
};
void printInfo(struct person *per)
{
printf("name = %s, age = %d, work = %s\n", per->name, per->age, per->work);
}
int main(int argc, char **argv)
{
struct person persons[] = {
{"zhangsan", 10, "teacher", printInfo},
{"lisi", 16, "doctor", printInfo},
};
persons[0].printInfo(&persons[0]);
persons[1].printInfo(&persons[1]);
return 0;
}
person5.cpp
cpp
#include <stdio.h>
struct person {
char *name;
int age;
char *work;
void printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
};
int main(int argc, char **argv)
{
struct person persons[] = {
{"zhangsan", 10, "teacher"},
{"lisi", 16, "doctor"},
};
persons[0].printInfo();
persons[1].printInfo();
return 0;
}
person6.cpp
cpp
#include <stdio.h>
class person {
public:
char *name;
int age;
char *work;
void printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
};
int main(int argc, char **argv)
{
struct person persons[] = {
{"zhangsan", 10, "teacher"},
{"lisi", 16, "doctor"},
};
persons[0].printInfo();
persons[1].printInfo();
return 0;
}
02th_accesscontrol
person.cpp
cpp
#include <stdio.h>
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
};
int main(int argc, char **argv)
{
Person per;
//per.name = "zhangsan";
per.setName("zhangsan");
per.setAge(200);
per.printInfo();
return 0;
}
person2.cpp
cpp
#include <stdio.h>
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name)
{
this->name = name;
}
int setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
};
int main(int argc, char **argv)
{
Person per;
//per.name = "zhangsan";
per.setName("zhangsan");
per.setAge(200);
per.printInfo();
return 0;
}
03th_ProgramStructure
01th
person.cpp
cpp
#include <stdio.h>
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Person::printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
int main(int argc, char **argv)
{
Person per;
//per.name = "zhangsan";
per.setName("zhangsan");
per.setAge(200);
per.printInfo();
return 0;
}
02th
person.h
cpp
#include <stdio.h>
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
person.cpp
cpp
#include <stdio.h>
#include "person.h"
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Person::printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
main.cpp
cpp
#include <stdio.h>
#include "person.h"
int main(int argc, char **argv)
{
Person per;
//per.name = "zhangsan";
per.setName("zhangsan");
per.setAge(200);
per.printInfo();
return 0;
}
Makefile
bash
person: main.o person.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o person
03th
person.h
cpp
#include <stdio.h>
namespace A {
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
person.cpp
cpp
#include <stdio.h>
#include "person.h"
namespace A {
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Person::printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
void printVersion(void)
{
printf("Person v1, by weidongshan\n");
}
}
dog.h
cpp
namespace C {
class Dog {
private:
char *name;
int age;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
dog.cpp
cpp
#include <stdio.h>
#include "dog.h"
namespace C {
void Dog::setName(char *name)
{
this->name = name;
}
int Dog::setAge(int age)
{
if (age < 0 || age > 20)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Dog::printInfo(void)
{
printf("name = %s, age = %d\n", name, age);
}
void printVersion(void)
{
printf("Dog v1, by weidongshan\n");
}
}
main.cpp
cpp
#include <stdio.h>
#include "person.h"
#include "dog.h"
int main(int argc, char **argv)
{
A::Person per;
per.setName("zhangsan");
per.setAge(16);
per.printInfo();
C::Dog dog;
dog.setName("wangcai");
dog.setAge(1);
dog.printInfo();
A::printVersion();
C::printVersion();
return 0;
}
Makefile
bash
person: main.o person.o dog.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o person
04th
person.h
cpp
#include <stdio.h>
namespace A {
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
person.cpp
cpp
#include <stdio.h>
#include "person.h"
namespace A {
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Person::printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
void printVersion(void)
{
printf("Person v1, by weidongshan\n");
}
}
dog.h
cpp
namespace C {
class Dog {
private:
char *name;
int age;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
dog.cpp
cpp
#include <stdio.h>
#include "dog.h"
namespace C {
void Dog::setName(char *name)
{
this->name = name;
}
int Dog::setAge(int age)
{
if (age < 0 || age > 20)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Dog::printInfo(void)
{
printf("name = %s, age = %d\n", name, age);
}
void printVersion(void)
{
printf("Dog v1, by weidongshan\n");
}
}
main.cpp
cpp
#include <stdio.h>
#include "person.h"
#include "dog.h"
/* global namespace */
/* 把A::Person放入global namespace, 以后可以使用Person来表示A::Person */
using A::Person;
/* 把C::Dog放入global namespace, 以后可以使用Dog来表示C::Dog */
using C::Dog;
using A::printVersion;
using C::printVersion;
int main(int argc, char **argv)
{
/* local namespace */
//using A::Person;
//using C::Dog;
Person per;
per.setName("zhangsan");
per.setAge(16);
per.printInfo();
Dog dog;
dog.setName("wangcai");
dog.setAge(1);
dog.printInfo();
A::printVersion();
C::printVersion();
return 0;
}
Makefile
bash
person: main.o person.o dog.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o person
05th
person.h
cpp
#include <stdio.h>
namespace A {
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
person.cpp
cpp
#include <stdio.h>
#include "person.h"
namespace A {
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Person::printInfo(void)
{
printf("name = %s, age = %d, work = %s\n", name, age, work);
}
void printVersion(void)
{
printf("Person v1, by weidongshan\n");
}
}
dog.h
cpp
namespace C {
class Dog {
private:
char *name;
int age;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
dog.cpp
cpp
#include <stdio.h>
#include "dog.h"
namespace C {
void Dog::setName(char *name)
{
this->name = name;
}
int Dog::setAge(int age)
{
if (age < 0 || age > 20)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Dog::printInfo(void)
{
printf("name = %s, age = %d\n", name, age);
}
void printVersion(void)
{
printf("Dog v1, by weidongshan\n");
}
}
main.cpp
cpp
#include <stdio.h>
#include "person.h"
#include "dog.h"
using namespace A;
using namespace C;
int main(int argc, char **argv)
{
/* local namespace */
//using A::Person;
//using C::Dog;
Person per;
per.setName("zhangsan");
per.setAge(16);
per.printInfo();
Dog dog;
dog.setName("wangcai");
dog.setAge(1);
dog.printInfo();
A::printVersion();
C::printVersion();
return 0;
}
Makefile
bash
person: main.o person.o dog.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o person
06th
person.h
cpp
#include <stdio.h>
namespace A {
class Person {
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
person.cpp
cpp
#include <iostream>
#include "person.h"
namespace A {
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int age)
{
if (age < 0 || age > 150)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Person::printInfo(void)
{
std::cout<<"name = "<<name<<" age = "<<age<<" work = "<<work<<std::endl;
}
void printVersion(void)
{
std::cout<<"Person v1, by weidongshan"<<std::endl;
}
}
dog.h
cpp
namespace C {
class Dog {
private:
char *name;
int age;
public:
void setName(char *name);
int setAge(int age);
void printInfo(void);
};
void printVersion(void);
}
dog.cpp
cpp
#include <iostream>
#include "dog.h"
namespace C {
using namespace std;
void Dog::setName(char *name)
{
this->name = name;
}
int Dog::setAge(int age)
{
if (age < 0 || age > 20)
{
this->age = 0;
return -1;
}
this->age = age;
return 0;
}
void Dog::printInfo(void)
{
cout<<"name = "<<name<<" age = "<<age<<endl;
}
void printVersion(void)
{
cout<<"Dog v1, by weidongshan"<<endl;
}
}
main.cpp
cpp
#include <stdio.h>
#include "person.h"
#include "dog.h"
using namespace A;
using namespace C;
int main(int argc, char **argv)
{
/* local namespace */
//using A::Person;
//using C::Dog;
Person per;
per.setName("zhangsan");
per.setAge(16);
per.printInfo();
Dog dog;
dog.setName("wangcai");
dog.setAge(1);
dog.printInfo();
A::printVersion();
C::printVersion();
return 0;
}
Makefile
bash
person: main.o person.o dog.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o person
04th_overload
main.cpp
cpp
#include <iostream>
using namespace std;
int add(int a, int b)
{
cout<<"add int+int"<<endl;
return a+b;
}
int add(int a, int b, int c)
{
cout<<"add int+int+int"<<endl;
return a+b+c;
}
double add(double a, double b)
{
cout<<"add double+double"<<endl;
return a+b;
}
double add(int a, double b)
{
cout<<"add int+double"<<endl;
return (double)a+b;
}
double add(double b, int a)
{
cout<<"add double+int"<<endl;
return (double)a+b;
}
int main(int argc, char **argv)
{
add(1, 2);
add(1, 2, 3);
add(1.0, 2.0);
add(1, 2.0);
add(1.0, 2);
return 0;
}
05th_pointer_reference
main.cpp
cpp
#include <iostream>
using namespace std;
int add_one(int a)
{
a = a+1;
return a;
}
int add_one(int *a)
{
*a = *a + 1;
return *a;
}
int add_one_ref(int &b)
{
b = b+1;
return b;
}
int main(int argc, char **argv)
{
int a = 99;
int &c = a;
cout<<add_one(a)<<endl;
cout<<"a = "<<a<<endl;
cout<<add_one(&a)<<endl;
cout<<"a = "<<a<<endl;
cout<<add_one_ref(a)<<endl;
cout<<"a = "<<a<<endl;
c++;
cout<<"a = "<<a<<endl;
cout<<"c = "<<c<<endl;
return 0;
}
06th_constructor
person.cpp
cpp
#include <iostream>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {cout <<"Pserson()"<<endl;}
Person(char *name)
{
cout <<"Pserson(char *)"<<endl;
this->name = name;
}
Person(char *name, int age)
{
cout <<"Pserson(char*, int)"<<endl;
this->name = name;
this->age = age;
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 16);
Person per2;
per.printInfo();
return 0;
}
person2.cpp
cpp
#include <iostream>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {cout <<"Pserson()"<<endl;}
Person(char *name)
{
cout <<"Pserson(char *)"<<endl;
this->name = name;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int)"<<endl;
this->name = name;
this->age = age;
this->work = work;
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 16);
Person per2; /* 调用无参构造函数 */
Person per3(); /* int fun(); */
per.printInfo();
return 0;
}
person3.cpp
cpp
#include <iostream>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {cout <<"Pserson()"<<endl;}
Person(char *name)
{
cout <<"Pserson(char *)"<<endl;
this->name = name;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int)"<<endl;
this->name = name;
this->age = age;
this->work = work;
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 16);
Person per2; /* 调用无参构造函数 */
Person per3(); /* int fun(); */
Person *per4 = new Person;
Person *per5 = new Person();
Person *per6 = new Person[2];
Person *per7 = new Person("lisi", 18, "student");
Person *per8 = new Person("wangwu", 18);
per.printInfo();
per7->printInfo();
per8->printInfo();
delete per4;
delete per5;
delete []per6;
delete per7;
delete per8;
return 0;
}
person4.cpp
cpp
#include <iostream>
#include <string.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {cout <<"Pserson()"<<endl;}
Person(char *name)
{
cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int)"<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 16);
Person per2; /* 调用无参构造函数 */
Person per3(); /* int fun(); */
Person *per4 = new Person;
Person *per5 = new Person();
Person *per6 = new Person[2];
Person *per7 = new Person("lisi", 18, "student");
Person *per8 = new Person("wangwu", 18);
per.printInfo();
per7->printInfo();
per8->printInfo();
delete per4;
delete per5;
delete []per6;
delete per7;
delete per8;
return 0;
}
person5.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
//cout <<"Pserson(char*, int)"<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
//cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
void test_fun()
{
Person per("zhangsan", 16);
Person per2; /* 调用无参构造函数 */
Person per3(); /* int fun(); */
Person *per4 = new Person;
Person *per5 = new Person();
Person *per6 = new Person[2];
Person *per7 = new Person("lisi", 18, "student");
Person *per8 = new Person("wangwu", 18);
per.printInfo();
per7->printInfo();
per8->printInfo();
delete per4;
delete per5;
delete []per6;
delete per7;
delete per8;
}
int main(int argc, char **argv)
{
for (int i = 0; i < 1000000; i++)
test_fun();
cout << "run test_fun end"<<endl;
sleep(10);
return 0;
}
person6.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
//cout <<"Pserson(char*, int)"<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
~Person()
{
if (this->name)
delete this->name;
if (this->work)
delete this->work;
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
//cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
void test_fun()
{
Person per("zhangsan", 16);
Person per2; /* 调用无参构造函数 */
Person per3(); /* int fun(); */
Person *per4 = new Person;
Person *per5 = new Person();
Person *per6 = new Person[2];
Person *per7 = new Person("lisi", 18, "student");
Person *per8 = new Person("wangwu", 18);
per.printInfo();
per7->printInfo();
per8->printInfo();
delete per4;
delete per5;
delete []per6;
delete per7;
delete per8;
}
int main(int argc, char **argv)
{
for (int i = 0; i < 1000000; i++)
test_fun();
cout << "run test_fun end"<<endl;
sleep(10);
return 0;
}
person7.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
//cout <<"Pserson(char*, int)"<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
//cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
void test_fun()
{
Person per("zhangsan", 16);
Person *per7 = new Person("lisi", 18, "student");
//delete per7;
}
int main(int argc, char **argv)
{
test_fun();
cout << "run test_fun end"<<endl;
sleep(10);
return 0;
}
person8.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
//cout <<"Pserson(char*, int)"<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 18);
Person per2(per);
per2.printInfo();
return 0;
}
person9.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
//cout <<"Pserson(char*, int)"<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 18);
Person per2(per);
per2.printInfo();
return 0;
}
person10.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
Person per_g("per_g", 10);
void func()
{
Person per_func("per_func", 11);
static Person per_func_s("per_func_s", 11);
}
int main(int argc, char **argv)
{
Person per_main("per_main", 11);
static Person per_main_s("per_main_s", 11);
for (int i = 0; i < 2; i++)
{
func();
Person per_for("per_for", i);
}
return 0;
}
person11.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {
cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
class Student {
private:
Person father;
Person mother;
int student_id;
public:
Student()
{
cout<<"Student()"<<endl;
}
Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39) : mother(mother, mother_age), father(father, father_age)
{
cout<<"Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)"<<endl;
}
~Student()
{
cout<<"~Student()"<<endl;
}
};
int main(int argc, char **argv)
{
Student s(100, "bill", "lily");
return 0;
}
07th_static
person.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
static int cnt;
char *name;
int age;
char *work;
public:
static int getCount(void)
{
return cnt;
}
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
cnt++;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
cnt++;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
cnt++;
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
cnt++;
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int Person::cnt = 0; /* 定义和初始化 */
int main(int argc, char **argv)
{
cout << "person number = "<<Person::getCount()<<endl;
return 0;
}
person2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
static int cnt;
char *name;
int age;
char *work;
public:
static int getCount(void);
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
cnt++;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
cnt++;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
cnt++;
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
cnt++;
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
int Person::cnt = 0; /* 定义和初始化 */
int Person::getCount(void)
{
return cnt;
}
int main(int argc, char **argv)
{
Person p[100];
cout << "person number = "<<Person::getCount()<<endl;
cout << "person number = "<<p[0].getCount()<<endl;
cout << "person number = "<<p[1].getCount()<<endl;
return 0;
}
08th_friend
point.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() {}
Point(int x, int y) : x(x), y(y) {}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
};
Point add(Point &p1, Point &p2)
{
Point n;
n.setX(p1.getX()+p2.getX());
n.setY(p1.getY()+p2.getY());
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);
Point sum = add(p1, p2);
sum.printInfo();
return 0;
}
point2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() {}
Point(int x, int y) : x(x), y(y) {}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);
Point sum = add(p1, p2);
sum.printInfo();
return 0;
}
09th_operator
person.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
static int cnt;
char *name;
int age;
char *work;
public:
static int getCount(void);
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
cnt++;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
cnt++;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
cnt++;
}
Person(const Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
cnt++;
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void) const
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
Person& operator=(const Person& p)
{
cout << "operator=(const Person& p)"<<endl;
if (this == &p)
return *this;
this->age = p.age;
if (this->name) {
delete this->name;
}
if (this->work) {
delete this->work;
}
this->name = new char[strlen(p.name) + 1];
strcpy(this->name, p.name);
this->work = new char[strlen(p.work) + 1];
strcpy(this->work, p.work);
return *this;
}
};
int Person::cnt = 0; /* 定义和初始化 */
int Person::getCount(void)
{
return cnt;
}
int main(int argc, char **argv)
{
const Person p1("zhangsan", 10);
cout<<"Person p2 = p1" <<endl;
Person p2 = p1;
Person p3;
cout<<"p3=p1"<<endl;
p3 = p1;
cout<<"end"<<endl;
p1.printInfo();
p2.printInfo();
p3.printInfo();
return 0;
}
point.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() {}
Point(int x, int y) : x(x), y(y) {}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
friend Point operator+(Point &p1, Point &p2);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);
Point sum = p1+p2;
sum.printInfo();
return 0;
}
point2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() {}
Point(int x, int y) : x(x), y(y) {}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
friend Point operator+(Point &p1, Point &p2);
friend Point operator++(Point &p);
friend Point operator++(Point &p, int a);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
/* Point p(1,2); ++p; */
Point operator++(Point &p)
{
cout<<"++p"<<endl;
p.x += 1;
p.y += 1;
return p;
}
/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
cout<<"p++"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);
Point n = ++p1;
n.printInfo();
p1.printInfo();
cout << "******************"<<endl;
Point m = p2++;
m.printInfo();
p2.printInfo();
return 0;
}
point3.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}
Point(const Point& p)
{
cout<<"Point(const Point& p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
friend Point operator+(Point &p1, Point &p2);
friend Point operator++(Point &p);
friend Point operator++(Point &p, int a);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
/* Point p(1,2); ++p; */
Point operator++(Point &p)
{
cout<<"++p"<<endl;
p.x += 1;
p.y += 1;
return p;
}
/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
cout<<"p++"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
cout<<"begin"<<endl;
++p1;
cout << "******************"<<endl;
p1++;
cout<<"end"<<endl;
return 0;
}
point4.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}
Point(const Point& p)
{
cout<<"Point(const Point& p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
friend Point operator+(Point &p1, Point &p2);
friend Point& operator++(Point &p);
friend Point operator++(Point &p, int a);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
cout<<"++p"<<endl;
p.x += 1;
p.y += 1;
return p;
}
/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
cout<<"p++"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
cout<<"begin"<<endl;
++p1;
cout << "******************"<<endl;
p1++;
cout<<"end"<<endl;
return 0;
}
point5.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}
Point(const Point& p)
{
cout<<"Point(const Point& p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
friend Point operator+(Point &p1, Point &p2);
friend Point& operator++(Point &p);
friend Point operator++(Point &p, int a);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
cout<<"++p"<<endl;
p.x += 1;
p.y += 1;
return p;
}
/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
cout<<"p++"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point m, n;
cout<<"begin"<<endl;
m = ++p1;
cout << "******************"<<endl;
n = p1++;
cout<<"end"<<endl;
return 0;
}
point6.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}
Point(const Point& p)
{
cout<<"Point(const Point& p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point add(Point &p1, Point &p2);
friend Point operator+(Point &p1, Point &p2);
friend Point& operator++(Point &p);
friend Point operator++(Point &p, int a);
friend ostream& operator<<(ostream &o, Point p);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
cout<<"++p"<<endl;
p.x += 1;
p.y += 1;
return p;
}
/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
cout<<"p++"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
ostream& operator<<(ostream &o, Point p)
{
cout<<"("<<p.x<<", "<<p.y<<")";
return o;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point m, n;
cout<<"begin"<<endl;
m = ++p1;
cout << "******************"<<endl;
n = p1++;
cout<<"end"<<endl;
cout<<m<<" "<<n<<endl;
return 0;
}
point7.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point()
{
cout<<"Point()"<<endl;
}
Point(int x, int y) : x(x), y(y)
{
cout<<"Point(int x, int y)"<<endl;
}
Point(const Point& p)
{
cout<<"Point(const Point& p)"<<endl;
x = p.x;
y = p.y;
}
~Point()
{
cout<<"~Point()"<<endl;
}
int getX(){ return x; }
int getY(){ return y; }
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
Point operator+(Point &p)
{
cout<<"operator+"<<endl;
Point n;
n.x = this->x + p.x;
n.y = this->y + p.y;
return n;
}
/* Point p(1,2); ++p; */
Point& operator++(void)
{
cout<<"operator++(void)"<<endl;
this->x += 1;
this->y += 1;
return *this;
}
/* Point p(1,2); p++; */
Point operator++(int a)
{
cout<<"operator++(int a)"<<endl;
Point n;
n = *this;
this->x += 1;
this->y += 1;
return n;
}
friend Point add(Point &p1, Point &p2);
friend ostream& operator<<(ostream &o, Point p);
};
Point add(Point &p1, Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
ostream& operator<<(ostream &o, Point p)
{
cout<<"("<<p.x<<", "<<p.y<<")";
return o;
}
int main(int argc, char **argv)
{
Point p1(1, 2);
Point p2(2, 3);
Point m, n;
m = p1 + p2; /* m = p1.operator+(p2); */
cout<<"add p1,p2 = "<<m<<endl;
cout<<"begin"<<endl;
m = ++p1; /* m = p1.operator++(); */
cout<<"m = "<<m<<" "<<"p1 = "<<p1<<endl;
cout << "******************"<<endl;
n = p1++; /* m = p1.operator++(0); */
cout<<"n = "<<n<<" "<<"p1 = "<<p1<<endl;
cout<<"end"<<endl;
return 0;
}
10th_inheritance
father_son.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
return 0;
}
father_son2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
/* 外人不能拿父亲的房间钥匙
* 儿子可以
*/
room_key = 1;
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
//s.room_key = 1;
return 0;
}
father_son3.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
//using Father::it_skill;
public:
using Father::room_key;
//using Father::money;
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
s.room_key = 1;
return 0;
}
father_son4.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
int address;
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son_pub : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pro : protected Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pri : private Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
int main(int argc, char **argv)
{
Son_pub s_pub;
Son_pro s_pro;
Son_pri s_pri;
s_pub.play_game();
s_pro.play_game();
s_pri.play_game();
s_pub.it_skill();
//s_pro.it_skill(); // error
//s_pri.it_skill(); // error
return 0;
}
father_son5.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
int address;
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son_pub : public Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pro : protected Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Son_pri : private Father {
private:
int toy;
public:
void play_game(void)
{
int m;
cout<<"son play game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
};
class Grandson_pub : public Son_pub {
public:
void test(void) {
room_key = 1; /* address is protected */
address = 2; /* address is public */
}
};
class Grandson_pro : public Son_pro {
public:
void test(void) {
room_key = 1; /* address is protected */
address = 2; /* address is protected */
}
};
class Grandson_pri : public Son_pri {
public:
void test(void) {
//room_key = 1; // error
//address = 2; // error
}
};
int main(int argc, char **argv)
{
Son_pub s_pub;
Son_pro s_pro;
Son_pri s_pri;
s_pub.play_game();
s_pro.play_game();
s_pri.play_game();
s_pub.it_skill();
//s_pro.it_skill(); // error
//s_pri.it_skill(); // error
Grandson_pub gs_pub;
Grandson_pro gs_pro;
gs_pub.address = 2;
//gs_pro.address = 2; // error
return 0;
}
father_son6.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Father {
private:
int money;
protected:
int room_key;
public:
void it_skill(void)
{
cout<<"father's it skill"<<endl;
}
int getMoney(void)
{
return money;
}
void setMoney(int money)
{
this->money = money;
}
};
class Son : public Father {
private:
int toy;
//using Father::it_skill;
public:
using Father::room_key;
//using Father::money;
void play_game(void)
{
int m;
cout<<"son paly game"<<endl;
/* money -= 1;
* 错: 不能直接拿父亲的私房钱
*/
/*
* 但是可以问他要
*/
m = getMoney();
m--;
setMoney(m);
room_key = 1;
}
/* 覆写 override */
void it_skill(void)
{
cout<<"son's it skill"<<endl;
}
};
int main(int argc, char **argv)
{
Son s;
s.setMoney(10);
cout << s.getMoney()<<endl;
s.it_skill();
s.play_game();
s.room_key = 1;
return 0;
}
person.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
static int cnt;
char *name;
int age;
public:
static int getCount(void);
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
cnt++;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
cnt++;
}
Person(char *name, int age)
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
cnt++;
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
cnt++;
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
}
void setName(char *name)
{
if (this->name) {
delete this->name;
}
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
cout<<"name = "<<name<<", age = "<<age<<endl;
}
};
int Person::cnt = 0; /* 定义和初始化 */
int Person::getCount(void)
{
return cnt;
}
class Student : public Person {
};
int main(int argc, char **argv)
{
Student s;
s.setName("zhangsan");
s.setAge(16);
s.printInfo();
return 0;
}
person_student.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
public:
int address;
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
Person(char *name, int age)
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
}
void setName(char *name)
{
if (this->name) {
delete this->name;
}
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
cout<<"name = "<<name<<", age = "<<age<<endl;
}
};
class Student : public Person {
private:
int grade;
void setGrade(int grade) {this->grade = grade;}
int getGrade(void) {return grade;}
public:
void printInfo(void)
{
cout<<"Student ";
Person::printInfo();
}
};
void test_func(Person &p)
{
p.printInfo();
}
int main(int argc, char **argv)
{
Person p("lisi", 16);
Student s;
s.setName("zhangsan");
s.setAge(16);
test_func(p);
test_func(s); /* Person &p = s里面的Person部分;
* p引用的是"s里面的Person部分"
*/
s.printInfo();
return 0;
}
11th_multi_inheritance
Sofabed.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Sofa {
public:
void watchTV(void) { cout<<"watch TV"<<endl; }
};
class Bed {
public:
void sleep(void) { cout<<"sleep"<<endl; }
};
class Sofabed : public Sofa, public Bed {
};
int main(int argc, char **argv)
{
Sofabed s;
s.watchTV();
s.sleep();
return 0;
}
Sofabed2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Sofa {
private:
int weight;
public:
void watchTV(void) { cout<<"watch TV"<<endl; }
void setWeight(int weight) { this->weight = weight; }
int getWeight(void) const { return weight; }
};
class Bed {
private:
int weight;
public:
void sleep(void) { cout<<"sleep"<<endl; }
void setWeight(int weight) { this->weight = weight; }
int getWeight(void) const { return weight; }
};
class Sofabed : public Sofa, public Bed {
};
int main(int argc, char **argv)
{
Sofabed s;
s.watchTV();
s.sleep();
//s.setWeight(100); /* error, 有二义性 */
s.Sofa::setWeight(100);
return 0;
}
Sofabed3.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Furniture {
private:
int weight;
public:
void setWeight(int weight) { this->weight = weight; }
int getWeight(void) const { return weight; }
};
class Sofa : virtual public Furniture {
private:
int a;
public:
void watchTV(void) { cout<<"watch TV"<<endl; }
};
class Bed : virtual public Furniture {
private:
int b;
public:
void sleep(void) { cout<<"sleep"<<endl; }
};
class Sofabed : public Sofa, public Bed {
private:
int c;
};
int main(int argc, char **argv)
{
Sofabed s;
s.watchTV();
s.sleep();
s.setWeight(100);
return 0;
}
12th_constructor_2
Sofabed.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Furniture {
private:
int weight;
public:
void setWeight(int weight) { this->weight = weight; }
int getWeight(void) const { return weight; }
public:
Furniture() { cout <<"Furniture()"<<endl; }
};
class Vertification3C {
public:
Vertification3C() { cout <<"Vertification3C()"<<endl; }
};
class Sofa : virtual public Furniture , virtual public Vertification3C{
private:
int a;
public:
void watchTV(void) { cout<<"watch TV"<<endl; }
public:
Sofa() { cout <<"Sofa()"<<endl; }
};
class Bed : virtual public Furniture, virtual public Vertification3C {
private:
int b;
public:
void sleep(void) { cout<<"sleep"<<endl; }
public:
Bed() { cout <<"Bed()"<<endl; }
};
class Sofabed : public Sofa, public Bed {
private:
int c;
public:
Sofabed() { cout <<"Sofabed()"<<endl; }
};
class LeftRightCom {
public:
LeftRightCom() { cout <<"LeftRightCom()"<<endl; }
};
class Date {
public:
Date() { cout <<"Date()"<<endl; }
};
class Type {
public:
Type() { cout <<"Type()"<<endl; }
};
class LeftRightSofabed : public Sofabed, public LeftRightCom {
private:
Date date;
Type type;
public:
LeftRightSofabed() { cout <<"LeftRightSofabed()"<<endl; }
};
int main(int argc, char **argv)
{
LeftRightSofabed s;
return 0;
}
Sofabed2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Furniture {
private:
int weight;
public:
void setWeight(int weight) { this->weight = weight; }
int getWeight(void) const { return weight; }
public:
Furniture() { cout <<"Furniture()"<<endl; }
};
class Vertification3C {
public:
Vertification3C() { cout <<"Vertification3C()"<<endl; }
};
class Sofa : virtual public Furniture , virtual public Vertification3C{
private:
int a;
public:
void watchTV(void) { cout<<"watch TV"<<endl; }
public:
Sofa() { cout <<"Sofa()"<<endl; }
};
class Bed : virtual public Furniture, virtual public Vertification3C {
private:
int b;
public:
void sleep(void) { cout<<"sleep"<<endl; }
public:
Bed() { cout <<"Bed()"<<endl; }
};
class Sofabed : public Sofa, public Bed {
private:
int c;
public:
Sofabed() { cout <<"Sofabed()"<<endl; }
};
class LeftRightCom {
public:
LeftRightCom() { cout <<"LeftRightCom()"<<endl; }
};
class Date {
public:
Date() { cout <<"Date()"<<endl; }
};
class Type {
public:
Type() { cout <<"Type()"<<endl; }
};
class LeftRightSofabed : public Sofabed, virtual public LeftRightCom {
private:
Date date;
Type type;
public:
LeftRightSofabed() { cout <<"LeftRightSofabed()"<<endl; }
};
int main(int argc, char **argv)
{
LeftRightSofabed s;
return 0;
}
Sofabed3.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Furniture {
private:
int weight;
public:
void setWeight(int weight) { this->weight = weight; }
int getWeight(void) const { return weight; }
public:
Furniture() { cout <<"Furniture()"<<endl; }
};
class Vertification3C {
public:
Vertification3C() { cout <<"Vertification3C()"<<endl; }
};
class Sofa : virtual public Furniture , virtual public Vertification3C{
private:
int a;
public:
void watchTV(void) { cout<<"watch TV"<<endl; }
public:
Sofa() { cout <<"Sofa()"<<endl; }
};
class Bed : virtual public Furniture, virtual public Vertification3C {
private:
int b;
public:
void sleep(void) { cout<<"sleep"<<endl; }
public:
Bed() { cout <<"Bed()"<<endl; }
};
class Sofabed : public Sofa, public Bed {
private:
int c;
public:
Sofabed() { cout <<"Sofabed()"<<endl; }
Sofabed(char *abc) { cout <<"Sofabed(char *abc)"<<endl; }
};
class LeftRightCom {
public:
LeftRightCom() { cout <<"LeftRightCom()"<<endl; }
LeftRightCom(char *abc) { cout <<"LeftRightCom(char *abc)"<<endl; }
};
class Date {
public:
Date() { cout <<"Date()"<<endl; }
Date(char *abc) { cout <<"Date(char *abc)"<<endl; }
};
class Type {
public:
Type() { cout <<"Type()"<<endl; }
Type(char *abc) { cout <<"Type(char *abc)"<<endl; }
};
class LeftRightSofabed : public Sofabed, virtual public LeftRightCom {
private:
Date date;
Type type;
public:
LeftRightSofabed() { cout <<"LeftRightSofabed()"<<endl; }
LeftRightSofabed(char *str1, char *str2, char *str3) : Sofabed(str1), LeftRightCom(str2), date(str3) { cout <<"LeftRightSofabed()"<<endl; }
};
int main(int argc, char **argv)
{
LeftRightSofabed s("abc", "2343", "yyy");
return 0;
}
13th_Polymorphism
Human.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
public:
void eating(void) { cout<<"use hand to eat"<<endl; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
};
void test_eating(Human& h)
{
h.eating();
}
int main(int argc, char **argv)
{
Human h;
Englishman e;
Chinese c;
test_eating(h);
test_eating(e);
test_eating(c);
return 0;
}
Human2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
};
void test_eating(Human& h)
{
h.eating();
}
int main(int argc, char **argv)
{
Human h;
Englishman e;
Chinese c;
test_eating(h);
test_eating(e);
test_eating(c);
cout<<"sizeof(Human) = "<<sizeof(h)<<endl;
cout<<"sizeof(Englishman) = "<<sizeof(e)<<endl;
cout<<"sizeof(Chinese) = "<<sizeof(c)<<endl;
return 0;
}
Human3.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
};
void test_eating(Human h)
{
h.eating();
}
int main(int argc, char **argv)
{
Human h;
Englishman e;
Chinese c;
test_eating(h);
test_eating(e);
test_eating(c);
cout<<"sizeof(Human) = "<<sizeof(h)<<endl;
cout<<"sizeof(Englishman) = "<<sizeof(e)<<endl;
cout<<"sizeof(Chinese) = "<<sizeof(c)<<endl;
return 0;
}
Human4.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
};
void test_eating(Human h)
{
h.eating();
}
int main(int argc, char **argv)
{
Human* h = new Human;
Englishman* e = new Englishman;
Chinese* c = new Chinese;
Human *p[3] = {h, e, c};
int i;
for (i = 0; i < 3; i++)
{
p[i]->eating();
delete p[i];
}
return 0;
}
Human5.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual void test(void) {cout<<"Human's test"<<endl; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual int test(void) {cout<<"Englishman's test"<<endl; return 1; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual int test(void) {cout<<"Chinese's test"<<endl; return 1; }
};
void test_eating(Human& h)
{
h.eating();
}
void test_return(Human& h)
{
h.test();
}
int main(int argc, char **argv)
{
Human h;
Englishman e;
Chinese c;
test_return(h);
test_return(e);
test_return(c);
return 0;
}
Human6.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
void test_eating(Human& h)
{
h.eating();
}
void test_return(Human& h)
{
h.test();
}
int main(int argc, char **argv)
{
Human h;
Englishman e;
Chinese c;
test_return(h);
test_return(e);
test_return(c);
return 0;
}
14th_convert
convert.c
cpp
#include <stdio.h>
int main(int argc, char **argv)
{
double d = 100.1;
int i = d; // doubleתΪint
char *str = "100ask.taobao.com";
int *p = str; // char *תΪint *
printf("i = %d, str = 0x%x, p = 0x%x\n", i, str, p);
return 0;
}
convert2.c
cpp
#include <stdio.h>
int main(int argc, char **argv)
{
double d = 100.1;
int i = d; // doubleתΪint
char *str = "100ask.taobao.com";
int *p = (int *)str; // char *תΪint *
printf("i = %d, str = 0x%x, p = 0x%x\n", i, (unsigned int)str, (unsigned int)p);
return 0;
}
convert3.cpp
cpp
#include <stdio.h>
int main(int argc, char **argv)
{
double d = 100.1;
int i = d; // doubleתΪint
char *str = "100ask.taobao.com";
int *p = reinterpret_cast<int *>(str); // char *תΪint *
printf("i = %d, str = 0x%x, p = 0x%x\n", i, reinterpret_cast<unsigned int>(str), reinterpret_cast<unsigned int>(p));
return 0;
}
convert4.cpp
cpp
#include <stdio.h>
int main(int argc, char **argv)
{
double d = 100.1;
int i = d; // doubleתΪint
const char *str = "100ask.taobao.com";
char *str2 = const_cast<char *>(str);
int *p = reinterpret_cast<int *>(str2); // char *תΪint *
printf("i = %d, str = 0x%x, p = 0x%x\n", i, reinterpret_cast<unsigned int>(str), reinterpret_cast<unsigned int>(p));
return 0;
}
convert5_Human.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
void test_eating(Human& h)
{
Englishman *pe;
Chinese *pc;
h.eating();
/* 想分辨这个"人"是英国人还是中国人? */
if (pe = dynamic_cast<Englishman *>(&h))
cout<<"This human is Englishman"<<endl;
if (pc = dynamic_cast<Chinese *>(&h))
cout<<"This human is Chinese"<<endl;
}
int main(int argc, char **argv)
{
Human h;
Englishman e;
Chinese c;
test_eating(h);
test_eating(e);
test_eating(c);
return 0;
}
convert6_Human.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
class Guangximan : public Chinese {
public:
void eating(void) { cout<<"use chopsticks to eat, I come from guangxi"<<endl; }
};
void test_eating(Human& h)
{
Englishman *pe;
Chinese *pc;
Guangximan *pg;
h.eating();
/* 想分辨这个"人"是英国人还是中国人? */
if (pe = dynamic_cast<Englishman *>(&h))
cout<<"This human is Englishman"<<endl;
if (pc = dynamic_cast<Chinese *>(&h))
cout<<"This human is Chinese"<<endl;
if (pg = dynamic_cast<Guangximan *>(&h))
cout<<"This human is Guangximan"<<endl;
}
int main(int argc, char **argv)
{
//Human h;
//Englishman e;
//Chinese c;
Guangximan g;
//test_eating(h);
//test_eating(e);
//test_eating(c);
test_eating(g);
return 0;
}
convert7_Human.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
class Guangximan : public Chinese {
public:
void eating(void) { cout<<"use chopsticks to eat, I come from guangxi"<<endl; }
};
void test_eating(Human& h)
{
#if 1
//Englishman& pe = dynamic_cast<Englishman&>(h);
Chinese& pc = dynamic_cast<Chinese&>(h);
Guangximan& pg = dynamic_cast<Guangximan&>(h);
#else
Englishman& pe = reinterpret_cast<Englishman&>(h);
Chinese& pc = reinterpret_cast<Chinese&>(h);
Guangximan& pg = reinterpret_cast<Guangximan&>(h);
#endif
h.eating();
}
int main(int argc, char **argv)
{
//Human h;
//Englishman e;
//Chinese c;
Guangximan g;
//test_eating(h);
//test_eating(e);
//test_eating(c);
test_eating(g);
return 0;
}
convert8_Human.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) { cout<<"use hand to eat"<<endl; }
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
class Guangximan : public Chinese {
public:
void eating(void) { cout<<"use chopsticks to eat, I come from guangxi"<<endl; }
};
void test_eating(Human& h)
{
#if 1
//Englishman& pe = dynamic_cast<Englishman&>(h);
Chinese& pc = dynamic_cast<Chinese&>(h);
Guangximan& pg = dynamic_cast<Guangximan&>(h);
#else
Englishman& pe = reinterpret_cast<Englishman&>(h);
Chinese& pc = reinterpret_cast<Chinese&>(h);
Guangximan& pg = reinterpret_cast<Guangximan&>(h);
#endif
h.eating();
}
int main(int argc, char **argv)
{
Human h;
//Englishman e;
//Chinese c;
Guangximan g;
Englishman *pe;
pe = static_cast<Englishman *>(&h);
//Englishman *pe2 = static_cast<Englishman *>(&g);
Chinese *pc = static_cast<Chinese *>(&g);
return 0;
}
15th_abstract
1th
Human.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
void wearing(void) {cout<<"wear english style"<<endl; }
void driving(void) {cout<<"drive english car"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
void wearing(void) {cout<<"wear chinese style"<<endl; }
void driving(void) {cout<<"drive chinese car"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
int main(int argc, char **argv)
{
//Human h;
Englishman e;
Chinese c;
return 0;
}
Human2.cpp
cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
void wearing(void) {cout<<"wear english style"<<endl; }
void driving(void) {cout<<"drive english car"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
void wearing(void) {cout<<"wear chinese style"<<endl; }
//void driving(void) {cout<<"drive chinese car"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
class Guangximan : public Chinese {
void driving(void) {cout<<"drive guangxi car"<<endl; }
};
int main(int argc, char **argv)
{
//Human h;
Englishman e;
Guangximan g;
return 0;
}
2th
Chinese.cpp
cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Chinese.h
cpp
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Chinese{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman.h
cpp
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Englishman {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
main.cpp
cpp
#include "Englishman.h"
#include "Chinese.h"
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
e.eating();
c.eating();
return 0;
}
Makefile
bash
Human: main.o Chinese.o Englishman.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o Human