This commit is contained in:
Light-City
2020-04-06 00:57:02 +08:00
parent f97c156cc4
commit a4d828bb4c
120 changed files with 4413 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
#include<iostream>
using namespace std;
void swap(int & a, int & b);
int main(int argc, char const *argv[])
{
int x1(5);
int x2(7);
swap(x1,x2);
cout<<x1<<" "<<x2<<endl;
system("pause");
return 0;
}
void swap(int & a, int & b)
{
int t;
t = a;
a = b;
b = t;
}

View File

@@ -0,0 +1,18 @@
#include<iostream>
using namespace std;
//函数声明
inline double CalArea(double radius);
int main(int argc, char const *argv[])
{
double r(3.0);
double area;
area = CalArea(r);
cout<<area<<endl;
system("pause");
return 0;
}
//加关键字inline
inline double CalArea(double radius)
{
return 3.14*radius*radius;
}

View File

@@ -0,0 +1,61 @@
/*
一圆型游泳池如图所示现在需在其周围建一圆型过道并在其四周围上栅栏。栅栏价格为35元/米过道造价为20元/平方米。
过道宽度为3米游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
图形描述:大圆嵌套小圆:
小圆在大圆中间,小圆为游泳池,大圆与小圆间隔为过道。
*/
#include<iostream>
using namespace std;
const float PI=3.14159;
const float FencePrice=35;
const float ConcretePrice=20;
class Circle
{
private:
float radius;
public:
Circle(float r);
float Circumference() const;
float Area() const;
};
Circle::Circle(float r)
{
radius=r;
}
// 计算圆的周长
float Circle::Circumference() const
{
return 2 * PI * radius;
}
// 计算圆的面积
float Circle::Area() const
{
return PI * radius * radius;
}
int main(int argc, char const *argv[])
{
float radius;
float FenceCost, ConcreteCost;
// 提示用户输入半径
cout<<"Enter the radius of the pool: ";
cin>>radius;
// 声明 Circle 对象
Circle Pool(radius);
Circle PoolRim(radius + 3);
// 计算栅栏造价并输出
FenceCost = PoolRim.Circumference() * FencePrice;
cout << "Fencing Cost is ¥" << FenceCost << endl;
// 计算过道造价并输出
ConcreteCost = (PoolRim.Area() - Pool.Area())*ConcretePrice;
cout << "Concrete Cost is ¥" << ConcreteCost << endl;
system("pause");
return 0;
}

View File

@@ -0,0 +1,40 @@
/*
使用前向引用声明虽然可以解决一些问题,但它并不是万能的。需要注意的是,
尽管使用了前向引用声明,但是在提供一个完整的类声明之前,不能声明该类的对象,
也不能在内联成员函数中使用该类的对象。请看下面的程序段:
*/
//第一种
#include<iostream>
class Fred; //前向引用声明
class Barney {
Fred x; //错误类Fred的声明尚不完善
};
class Fred {
Barney y;
};
//第二种
class Fred; //前向引用声明
class Barney {
public:
void method()
{
x->yabbaDabbaDo(); //错误Fred类的对象在定义之前被使用
}
private:
Fred* x; //正确经过前向引用声明可以声明Fred类的对象指针
};
class Fred {
public:
void yabbaDabbaDo();
private:
Barney* y;
};
/*
总结:当使用前向引用声明时,只能使用被声明的符号,而不能涉及类的任何细节。
*/

View File

@@ -0,0 +1,30 @@
/*
知识点:
静态成员函数
类外代码可以使用类名和作用域操作符来调用静态成员函数。
静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
*/
#include<iostream>
using namespace std;
class Application
{
public:
static void f();
static void g();
private:
static int global;
};
int Application::global=0;
void Application::f()
{ global=5;}
void Application::g()
{ cout<<global<<endl;}
int main()
{
Application::f();
Application::g();
system("pause");
return 0;
}

View File

@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;
class A
{
public:
static void f(A a);
private:
int x;
};
void A::f(A a)
{
//静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
// cout<<x; //对x的引用是错误的
cout<<a.x; //正确
}
int main(int argc, char const *argv[])
{
A a;
a.f(A());
system("pause");
return 0;
}

View File

@@ -0,0 +1,39 @@
/*
学习知识:
静态数据成员
用关键字static声明
该类的所有对象维护该成员的同一个拷贝
必须在类外定义和初始化,用(::)来指明所属的类。
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(int xx=0, int yy=0) {X=xx; Y=yy; countP++; }
Point(Point &p);
int GetX() {return X;}
int GetY() {return Y;}
void GetC() {cout<<" Object id="<<countP<<endl;}
private:
int X,Y;
//静态数据成员,必须在外部定义和初始化,内部不能直接初始化!
static int countP;
};
Point::Point(Point &p)
{ X=p.X;
Y=p.Y;
countP++;
}
//必须在类外定义和初始化,用(::)来指明所属的类。
int Point::countP=0;
int main()
{ Point A(4,5);
cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
A.GetC();
Point B(A);
cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
B.GetC();
system("pause");
return 0;
}