C++学习笔记之结构体与类的区别

来源:互联网 发布:网络推广工资一般多少 编辑:程序博客网 时间:2024/05/01 09:34

1、在C++中的, 结构体具有类的全部功能, 区别在于结构体成员默认为public, 而类的成员默认为private。

#include<iostream>
using namespace std;
class A //将class换成struct完全没问题。
{
public:
    int get(){return i;}
    void set(int x){ i=x; }
private:
    int i;
};
int main()
{
    A a;
    a.set(100);
    cout<<a.get()<<endl;
    return 0;
}

//----------------------------------------------------------------------------------------------------------------

#include<iostream>
using namespace std;
void check(bool s);
struct people //将class换成struct完全没问题。
{
    double weight;
    double tall;
    char*name;
    char*native;
    bool sex;
};

int main()
{
    people Jack=
    {
        180.5,
            175.8,
            "Jack",
            "杭州",
            1
    } ;
    cout<<Jack.weight<<endl;
    cout<<Jack.tall<<endl;
    cout<<Jack.name<<endl;
    cout<<Jack.native<<endl;
    check(Jack.sex);
    cout<<endl;
    return 0;
}

void check(bool s){if (s==1)cout<<"男";else cout<<"女";}

-----------------------------------------------------------------------------------------------------------------

//struct与构造函数。
#include<iostream>
#include <string>
using namespace std;
void check(bool s);
struct people
{
    people(double t_weight, double t_tall, string name, string native, bool t_sex);
    double weight;
    double tall;
    string name;//
    string native;//
    bool sex;
};
void check(bool s){if (s==1)cout<<"男";else cout<<"女";}
int main()
{
    people Jack
        (
        180.5,
        175.8,
        "Jack",
        "杭州",
        1
        ) ;
    cout<<Jack.weight<<endl;
    cout<<Jack.tall<<endl;
    cout<<Jack.name<<endl;
    cout<<Jack.native<<endl;
    check(Jack.sex);
    cout<<endl;
    return 0;
}
people::people(double t_weight, double t_tall, string t_name, string t_native, bool t_sex)
{
    weight = t_weight;
    tall = t_tall;
    name = t_name;
    native = t_native;
    sex = t_sex;
}

//-------------------------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------
//struct是一种数据类型, 可以对其直接赋值, 但必须类型是一致的。
#include<iostream>
using namespace std;
struct people
{
    double weight;
    double tall;
};
int main()
{
    people Jack = {140.5, 175.5};
    people Mike = {120.6, 170.5};
    Mike = Jack;
    cout<<Jack.weight<<" "<<Jack.tall<<endl;
    cout<<Mike.weight<<" "<<Mike.tall<<endl;
    return 0;
}

//---------------------------------------------------------------------------------------------
//struct是一种数据类型, 可以将结构体传递给函数, 或者是返回结构体。
#include<iostream>
using namespace std;
struct time
{
    int hour;
    int minute;
};
const int perhour=60;
time sum(time t1, time t2);
void show(time t){cout<<t.hour<<":"<<t.minute<<endl;}
int main()
{
    time one={8, 15};
    time two={6, 55};
    time day=sum(one , two);
    cout<<"二天时间总计:";
    show(day);
    time day3={9, 35};
    cout<<"三天时间总计:";
    show(sum(day, day3));
    return 0;
}
time sum(time t1, time t2)
{
    time total;
    total.minute=(t1.minute+t2.minute)%perhour;
    total.hour=t1.hour+t2.hour+(t1.minute+t2.minute)%perhour;
    return total;
}

//使用指针, 返回地址. 这样做的好处是由于返回的是地址, 就不需调用复制构造函数, 结省内存开销。
#include<iostream>
using namespace std;
struct time
{
    int hour;
    int minute;
};
const int perhour=60;
time *sum(time t1, time t2);
void show(time t){cout<<t.hour<<":"<<t.minute<<endl;}
int main()
{
    time one={8, 15};
    time two={6, 55};
    time *day=sum(one , two);
    cout<<"二天时间总计:";
    show(*day);
    time day3={9, 35};
    cout<<"三天时间总计:";
    time *p=sum(*day, day3);
    show(*p);
    return 0;
}
time *sum(time t1, time t2)
{
    time *total = new time;
    total->minute=(t1.minute+t2.minute)%perhour;
    total->hour=t1.hour+t2.hour+(t1.minute+t2.minute)%perhour;
    return total;
}

//--------------------------------------------------------------------------------
//结构体与String类
//多个字符串传递给函数。
#include<iostream>
#include <string>
using namespace std;
void show(string str[], int count);
int main()
{
    const int length = 5;
    string str1[length];
    for(int i=0; i<length; i++)
    {
        cout<<i+1<<":";
        cin>>str1[i];
    }
    show(str1, length);
    return 0;
}
void show(string str[], int count)
{
    for (int i=0; i<count; i++)
    {
        cout<<i+1<<":"<<str[i]<<endl;
    }
}

原创粉丝点击