使用Windows窗口
#include<stdio.h>
#include<Windows.h>
int main()
{
MessageBox(NULL, "Mike can drive!", "Driving", MB_YESNO);
return 0;
}
程序运行的结果如下图所示:
使用面向过程设计方法计算圆的周长和面积
#include<stdio.h>
int main()
{
double r = 0, girth, area;
const double PI = 3.14;
scanf("%lf", &r);
girth = 2 * PI * r;
area = PI * r * r;
printf("%lf,%lf", girth, area);
while (1);
return 0;
}
程序的运行结果如图示:
同样的功能采用C++语言的方式实现
#include<iostream>
int main()
{
double r = 0, girth, area;
const double PI = 3.14;
std::cin >> r;
girth = 2 * PI * r;
area = PI * r * r;
std::cout << girth << std::endl;
std::cout << area << std::endl;
while (1);
return 0;
}
运行的结构如图所示:
若采用C++的面向对象设计则程序如下:
#include<iostream>
class Circle {
double radius;
public:
double get_radius(double r)
{
radius = r;
return 0;
}
double get_girth()
{
return 2 * 3.14*radius;
}
double get_area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle A;
A.get_radius(5);
std::cout << "The circle's girth is " << A.get_girth() << std::endl;
std::cout << "The circle's area is " << A.get_area() << std::endl;
while (1);
return 0;
}
程序运行的结构如下:
The circle's girth is 31.4
The circle's area is 78.5
虽然此时程序稍微复杂了一点,但是程序的复用性很高,例如可以直接计算多个圆的相关参数:
int main()
{
Circle A,B,C;
A.get_radius(5);
std::cout << "The circle's girth is " << A.get_girth() << std::endl;
std::cout << "The circle's area is " << A.get_area() << std::endl;
B.get_radius(10);
std::cout << "The circle's girth is " << B.get_girth() << std::endl;
std::cout << "The circle's area is " << B.get_area() << std::endl;
C.get_radius(15);
std::cout << "The circle's girth is " << C.get_girth() << std::endl;
std::cout << "The circle's area is " << C.get_area() << std::endl;
while (1);
return 0;
}
程序的执行结构如下:
The circle's girth is 31.4
The circle's area is 78.5
The circle's girth is 62.8
The circle's area is 314
The circle's girth is 94.2
The circle's area is 706.5
关于公有,私有和保护成员
#include<iostream>
using namespace std;
class animal
{
public:
char people[10];
char lion[10];
char glede[10];
};//类的主体是在{}中间,最后需要加上一个分号
class MyClass
{
//在三者之外即未写修饰符则默认为私有的
//修饰符的作用域是从修饰符开始到下一个访问修饰符或最后
public:
//公开的,公共的
int a = 10;
void print();
private:
//私有的,类里的成员和函数才可以访问
protected:
//受保护的,所继承的派生类里可以访问
};
void MyClass::print()
{
cout << "hello world" << endl;
}
int main()
{
MyClass A;
cout << A.a << endl;
A.print();
getchar();
return 0;
}
程序的输出结果如下:
10
hello world
若在私有或者保护成员里面添加项,用对象是调用不了的。
构造函数
class Time
{
public:
Time();//构造函数声明,函数名与类名一致,没有类型也没有返回值
};
Time::Time()
{
cout << "This is a constructor" << endl;
}
int main()
{
Time t;
getchar();
return 0;
}
输出结果:
This is a constructor
有参构造
#include<iostream>
using namespace std;
class Max
{
public:
Max(int a, int b);
//如果不屑构造函数,编译器会自动生成,如果写了,则调用你写的构造函数
//构造函数分为有参和无参两种情况,如果是有参构造,在创建对象的时候记得也要传递参数
int MaxFunc();
private:
int x, y;
};
Max::Max(int a, int b)
{
cout << "This is parameter_constructor" << endl;
x = a;
y = b;
}
int Max::MaxFunc()
{
return x > y ? x : y;
}
int main()
{
Max m(3,6);
cout << "the biggest" << m.MaxFunc() << endl;
getchar();
return 0;
}
程序的输出结果是:
This is parameter_constructor
the biggest6
构造函数的重载
#include<iostream>
using namespace std;
class Max
{
public:
Max(int a, int b);//两个参数的构造函数
Max(int a, int b, int c);//三个参数的构造函数
int MaxFunc1();
int MaxFunc2();
private:
int x;
int y;
int z;
};
Max::Max(int a, int b)
{
cout << "This is two parameter_constructor" << endl;
x = a;
y = b;
}
Max::Max(int a, int b, int c)
{
cout << "This is three parameter_constructor" << endl;
x = a;
y = b;
z = c;
}
int Max::MaxFunc1()
{
return x > y ? x : y;
}
int Max::MaxFunc2()
{
return x > y ? (x > z ? x : z) : (y > z ? y : z);
}
int main()
{
Max m1(3, 6);
cout << "the biggest" << m1.MaxFunc1() << endl;
Max m2(3, 6, 9);
cout << "the biggest" << m2.MaxFunc2() << endl;
getchar();
return 0;
}
程序的运行结果是:
This is two parameter_constructor
the biggest6
This is three parameter_constructor
the biggest9
拷贝构造函数
#include<iostream>
using namespace std;
class number
{
public:
number();//默认构造函数
number(int i);//有参构造函数
number(number©);//拷贝构造函数
void print();//输出函数
void print(number obj);//重载输出函数
private:
int *p;
};
number::number()
{
cout << "I am default constructor!" << endl;
}
number::number(int i)
{
cout << "I am parameter constructor!" << endl;
p = new int;//给指针p申请内存
*p = i;//形参赋值给地址p中的值
}
number::number(number©)//传递的是一个对象
{
cout << "I am copy constructor!" << endl;
p = new int;
*p = *copy.p;//值拷贝
//copy是形参,*copy.p即为传入对象的值
}
void number::print()
{
cout << "value:" << *p << endl;
}
void number::print(number obj)
{
cout << "value:" << *obj.p << endl;
}
int main()
{
number n1;
number n2(5);
n2.print();
number n3(10);
n3.print();
number n4(n2);
n4.print();
number n5(n3);
n5.print();
getchar();
return 0;
}
程序的运行结果是:
I am default constructor!
I am parameter constructor!
value:5
I am parameter constructor!
value:10
I am copy constructor!
value:5
I am copy constructor!
value:10
析构函数
class MyClass
{
public:
MyClass();//构造函数 申请内存 成员函数 没有类型 没有返回值
~MyClass();//析构函数 释放内存 成员函数 没有类型 没有返回值 没有参数
};
MyClass::MyClass()
{
}
MyClass::~MyClass()
{
}
例如在上述的程序中添加如下部分:
~number();
number::~number()
{
cout << "I will delete the storage!" << endl;
}
//去掉getchar()
则控制台运行的结果变为:
I am default constructor!
I am parameter constructor!
value:5
I am parameter constructor!
value:10
I am copy constructor!
value:5
I am copy constructor!
value:10
I will delete the storage!
I will delete the storage!
I will delete the storage!
I will delete the storage!
I will delete the storage!
请按任意键继续. . .
出现四次的原因是因为创建了四次对象
友元
#include<iostream>
using namespace std;
class computer
{
public:
void getPrice(double f);//普通成员函数,用来获取电脑的价格
friend void print(computer PC);//友元函数
private:
double Price;
};
//普通成员函数,用来获取电脑的价格
void computer::getPrice(double f)
{
Price = f;
}
//友元函数不是成员函数,不需要加作用域
void print(computer PC)
{
cout << "the price is:" << PC.Price<<endl;
}
int main()
{
computer PC;
PC.getPrice(10000);
print(PC);
return 0;
}
程序运行的结果是:
the price is:10000
请按任意键继续. . .
友元类
#include<iostream>
using namespace std;
class computer
{
public:
char Name[20] = "ThinkPad";
void getPrice(double f);
friend class Mycomputer;
private:
double Price=10000;
};
void computer::getPrice(double f)
{
Price = f;
}
class Mycomputer
{
public:
computer PC;
void print();
};
void Mycomputer::print()
{
cout << "My computer brand is:" << PC.Name << endl;//Name是公共的
cout << "My computer price is:" << PC.Price << endl;
}
int main()
{
Mycomputer pc;
pc.print();
return 0;
}
程序的执行结果如下:
My computer brand is:ThinkPad
My computer price is:10000
请按任意键继续. . .