c++学习(四)

开始涉及类和对象了

/*#include"pch.h"
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define ElemType int

//typedef int ElemType;
typedef struct
{
    ElemType *elem;
    int length;
}SqList;
Status InitList(SqList )
int main()
{
    return 0;
}*/
/*#include"pch.h"
#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}
int main()
{
    int x;
    int y;
    cout << "please input your number that you want to swap:" << endl;
    cin >> x >> y;
    cout << "x=" << x << "y=" << y << endl;
    swap(x, y);
    cout << "x=" << x << "y=" << y;
    return 0;
}*/
/*#include"pch.h"
#include<iostream>
using namespace std;
const double PI = 3.14159265358979;
inline  double calArea(double radius) {
    return PI * radius*radius;
}
int main() {
    double r = 3.0;
    double area = calArea(r);
    cout << area << endl;
    return 0;
}*/
/*#include"pch.h"
#include<iomanip>
#include<iostream>
using namespace std;
int getVolume(int length, int width = 2, int height = 3);
int main() {
    int x, y, z;
    cout << "please input x,y,z" << endl;
    cin >> x >> y >> z;
    cout << "some body is:" << endl;

    cout << getVolume(x, y, z) << endl;
    cout << "some body data is:" << endl;
    cout << getVolume(x, y) << endl;
    cout << "some body data is:" << endl;
    cout << getVolume(x) << endl;
    return 0;
}
int getVolume(int length, int width, int height)
{
    cout << setw(5) << length << setw(5) << width << setw(5) << height << '\t';
    return length * width*height;
}*/
//函数重载
//同样的函数是有不同的参数类型,或者是不同的形参的个数,编译器不以返回值类型和形参名来区别不同的函数
/*#include"pch.h"
#include<iostream>
using namespace std;
int  sumOfSquare(int a, int b)
{
    return a * a + b * b;
 }
double sumOfSquare(double a, double b)
{
    return a * a + b * b;
}
int main()
{
    int n, m;
    cout << "please input your numbers:" << endl;
    cin >> n >> m;
    cout << sumOfSquare(n, m) << endl;
    double x, y;
    cout << "please input your numbers:" << endl;
    cin >> x >> y;
    cout << sumOfSquare(x, y) << endl;
    return 0;
}*/
/*#include"pch.h"
#include<iostream>
using namespace std;
float Convert(float F)
{
    float c;
    c = (F - 32) * 5 / 9;
    return c;
}
int main()
{
    float x;
    cout << "please input your temper:" << endl;
    cin >> x;
    cout << Convert(x) << endl;
    return 0;
}*/
/*class Clock {
public:
    void setTime(int newH, int newM, int newS);
    void showTime();
private:
    int hour=0, minute=0, second=0;//类内初始值
};*/
//内联成员函数 
//可以提高运行的效率,对于较简单的函数可以声明为内联形式
//内联函数体内不要有复杂结构
//如循环语句和switch 将函数放在类的声明中,使用inline关键字
#include"pch.h"
#include<iostream>
using namespace std;
class Clock {
public:
    void setTime(int newH = 0, int newM = 0, int newS = 0);
    void showTime( );
private:
    int hour, minute, second;
};
void Clock::setTime(int newH, int newM, int newS) {
    hour = newH;
    minute = newM;
    second = newS;
}
void Clock::showTime( ) {
    cout << hour << ":" << minute << ":" << second;
}
int main()
{
    int x, y, z;
    cout << "please input:";
    cin >> x >> y >> z;
    Clock myClock;
    myClock.setTime(x, y, z);
    myClock.showTime();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chuxinbubian/p/10140269.html