C++ day2 (思维导图)

C++ day2 (思维导图)_第1张图片

 作业:

        定义一个Student结构体,里面的成员有公有成员name、age,私有成员:score,从堆区连续分配3个结构体大小的空间,从键盘上输入3个学生的信息,分别存放到对应的位置上,按成绩的升序排序后输出三名学生的信息。

#include 

using namespace std;
#define N 5
struct student
{
    string name;
    int age;
    void show()
    {
        cout<>name;
        cout <<"输入学生的年龄";
        cin>>age;
        cout <<"输入该学生成绩";
        m.set_score();
    }
    struct mm
    {
    private:
        float score;
    public:
        float res()
        {
            return score;
        }
        void set_score()
        {
            cin>>score;
        }
    }m;
};

void sort(student mem[])
{
    student temp;
    int count=0;
    for(int i=1;imem[j+1].m.res())
            {
                temp=mem[j];
                mem[j]=mem[j+1];
                mem[j+1]=temp;
                count++;
            }
        }
        if(count==0) break;
    }

}


int main()
{
    cout << "Hello World!" << endl;
    student *member=new student[N];
    for(int i=0;i>>>"<

 1:引用refence

#include 

using namespace std;
int p;
int fun1()
{
    return p;
}
int *fun2()
{
    return &p;
}
int &fun3()
{
    return p;
}


int main()
{
    cout << "Hello World!" << endl;

    //fun1()=20;             //鏅€氬嚱鏁颁笉鑳借祴鍊
    //fun2()=30;             //鎸囬拡鍑芥暟涓嶈兘璧嬪€
    fun3()=40;
    cout <
#include 

using namespace std;
#define N 10
void sort(int (&a)[N])
{
    int temp;
    int count;
    for( int i=1;ia[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                count++;
            }
        }
        if(count ==0)
            break;
    }
}

int main()
{
    int a[N]={4,45,1,4,54,32,54,45,7,99};
    for(int i=0;i

2、new/delete


#include 
#define N 5
using namespace std;

void sort(int *&a)
{
    int temp;
    int count;
    for( int i=1;ia[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                count++;
            }
        }
        if(count == 0)
            break;
    }
}

int main()
{
    cout << "Hello World!" << endl;


    int *p1=new int;


    cout << "p1  = "<< p1 << endl;
    cout << "*p1 = " << *p1 << endl;

    int *p2 = new int(90);

    cout << "****************" << endl;
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    delete(p2);
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    int *ptr=new int[N];
    for(int i=0;i>ptr[i];
    }
    for(int i=0;i

3、overload


#include 
#define N 5
using namespace std;

void sort(int *&a)
{
    int temp;
    int count;
    for( int i=1;ia[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                count++;
            }
        }
        if(count == 0)
            break;
    }
}

int main()
{
    cout << "Hello World!" << endl;


    int *p1=new int;


    cout << "p1  = "<< p1 << endl;
    cout << "*p1 = " << *p1 << endl;

    int *p2 = new int(90);

    cout << "****************" << endl;
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    delete(p2);
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    int *ptr=new int[N];
    for(int i=0;i>ptr[i];
    }
    for(int i=0;i

4、struct

#include 

using namespace std;


struct NUM
{
public:         //不写默认为public,可以自己定义为private
    int age;
    string name;
    struct N
    {
    private:   //定义为了private
        int fat=0;
    public:   //需要定义外部接口来访问fat
        void show()
        {
            cout<>fat;
        }
    }n;
    void  show();

};
void NUM::show()
{
    cout<

你可能感兴趣的:(C++,c++,算法,开发语言)