使用Windows窗口
#include<stdio.h>
#include<Windows.h>
int main()
{
MessageBox(NULL, "Mike can drive!", "Driving", MB_YESNO);
return 0;
}
程序運(yùn)行的結(jié)果如下圖所示:
001.JPG
使用面向過程設(shè)計(jì)方法計(jì)算圓的周長和面積
#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;
}
程序的運(yùn)行結(jié)果如圖示:
002.JPG
同樣的功能采用C++語言的方式實(shí)現(xiàn)
#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;
}
運(yùn)行的結(jié)構(gòu)如圖所示:
003.JPG
若采用C++的面向?qū)ο笤O(shè)計(jì)則程序如下:
#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;
}
程序運(yùn)行的結(jié)構(gòu)如下:
The circle's girth is 31.4
The circle's area is 78.5
雖然此時(shí)程序稍微復(fù)雜了一點(diǎn)向楼,但是程序的復(fù)用性很高脯宿,例如可以直接計(jì)算多個(gè)圓的相關(guān)參數(shù):
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;
}
程序的執(zhí)行結(jié)構(gòu)如下:
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
關(guān)于公有馋袜,私有和保護(hù)成員
#include<iostream>
using namespace std;
class animal
{
public:
char people[10];
char lion[10];
char glede[10];
};//類的主體是在{}中間柳击,最后需要加上一個(gè)分號(hào)
class MyClass
{
//在三者之外即未寫修飾符則默認(rèn)為私有的
//修飾符的作用域是從修飾符開始到下一個(gè)訪問修飾符或最后
public:
//公開的弥雹,公共的
int a = 10;
void print();
private:
//私有的鹦筹,類里的成員和函數(shù)才可以訪問
protected:
//受保護(hù)的秋秤,所繼承的派生類里可以訪問
};
void MyClass::print()
{
cout << "hello world" << endl;
}
int main()
{
MyClass A;
cout << A.a << endl;
A.print();
getchar();
return 0;
}
程序的輸出結(jié)果如下:
10
hello world
若在私有或者保護(hù)成員里面添加項(xiàng)介衔,用對(duì)象是調(diào)用不了的恨胚。
構(gòu)造函數(shù)
class Time
{
public:
Time();//構(gòu)造函數(shù)聲明,函數(shù)名與類名一致,沒有類型也沒有返回值
};
Time::Time()
{
cout << "This is a constructor" << endl;
}
int main()
{
Time t;
getchar();
return 0;
}
輸出結(jié)果:
This is a constructor
有參構(gòu)造
#include<iostream>
using namespace std;
class Max
{
public:
Max(int a, int b);
//如果不屑構(gòu)造函數(shù)炎咖,編譯器會(huì)自動(dòng)生成赃泡,如果寫了,則調(diào)用你寫的構(gòu)造函數(shù)
//構(gòu)造函數(shù)分為有參和無參兩種情況乘盼,如果是有參構(gòu)造升熊,在創(chuàng)建對(duì)象的時(shí)候記得也要傳遞參數(shù)
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;
}
程序的輸出結(jié)果是:
This is parameter_constructor
the biggest6
構(gòu)造函數(shù)的重載
#include<iostream>
using namespace std;
class Max
{
public:
Max(int a, int b);//兩個(gè)參數(shù)的構(gòu)造函數(shù)
Max(int a, int b, int c);//三個(gè)參數(shù)的構(gòu)造函數(shù)
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;
}
程序的運(yùn)行結(jié)果是:
This is two parameter_constructor
the biggest6
This is three parameter_constructor
the biggest9
拷貝構(gòu)造函數(shù)
#include<iostream>
using namespace std;
class number
{
public:
number();//默認(rèn)構(gòu)造函數(shù)
number(int i);//有參構(gòu)造函數(shù)
number(number©);//拷貝構(gòu)造函數(shù)
void print();//輸出函數(shù)
void print(number obj);//重載輸出函數(shù)
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申請內(nèi)存
*p = i;//形參賦值給地址p中的值
}
number::number(number©)//傳遞的是一個(gè)對(duì)象
{
cout << "I am copy constructor!" << endl;
p = new int;
*p = *copy.p;//值拷貝
//copy是形參,*copy.p即為傳入對(duì)象的值
}
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;
}
程序的運(yùn)行結(jié)果是:
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
析構(gòu)函數(shù)
class MyClass
{
public:
MyClass();//構(gòu)造函數(shù) 申請內(nèi)存 成員函數(shù) 沒有類型 沒有返回值
~MyClass();//析構(gòu)函數(shù) 釋放內(nèi)存 成員函數(shù) 沒有類型 沒有返回值 沒有參數(shù)
};
MyClass::MyClass()
{
}
MyClass::~MyClass()
{
}
例如在上述的程序中添加如下部分:
~number();
number::~number()
{
cout << "I will delete the storage!" << endl;
}
//去掉getchar()
則控制臺(tái)運(yùn)行的結(jié)果變?yōu)椋?/p>
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!
請按任意鍵繼續(xù). . .
出現(xiàn)四次的原因是因?yàn)閯?chuàng)建了四次對(duì)象
友元
#include<iostream>
using namespace std;
class computer
{
public:
void getPrice(double f);//普通成員函數(shù)绸栅,用來獲取電腦的價(jià)格
friend void print(computer PC);//友元函數(shù)
private:
double Price;
};
//普通成員函數(shù)级野,用來獲取電腦的價(jià)格
void computer::getPrice(double f)
{
Price = f;
}
//友元函數(shù)不是成員函數(shù),不需要加作用域
void print(computer PC)
{
cout << "the price is:" << PC.Price<<endl;
}
int main()
{
computer PC;
PC.getPrice(10000);
print(PC);
return 0;
}
程序運(yùn)行的結(jié)果是:
the price is:10000
請按任意鍵繼續(xù). . .
友元類
#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;
}
程序的執(zhí)行結(jié)果如下:
My computer brand is:ThinkPad
My computer price is:10000
請按任意鍵繼續(xù). . .