[C++ Study Notes 7] Experiment 7- Advanced Class and Object Knowledge (1)

[Description]
Please complete the code by supplementing the implementation part of the class according to the operation of the class in the main function.
This class has a private static variable count to record the number of all objects of this class. The main function will output the number of objects after different statements. Only by implementing this class correctly and ensuring that count correctly records the number of objects of this class can the correct result be output .
【Input】
No input.
[Output]
The output of the main function has been written.

#include <iostream>
#include <string>
using namespace std;
class Student {
    
    
private:
    int id;
    static int count; 
public:



/* 请在此处编写相关代码 */

static void InitCount(){
    
    
    count=0;
}

Student(){
    
    
    count++;
}
Student(int a){
    
    //如果输入一个整数
    id=a;
    count++;
}
Student(const Student &stu){
    
    //复制构造函数,构造一个对象,count+1
    id=stu.id;
    count++;
}
~Student(){
    
    //析构完一个,count-1
    count--;
}

friend void PrintCount();//用友元,可以直接访问对象的私有数据成员count
friend void Print(Student s);//可以直接访问私有成员s2.id,无需get,数据共享

/* 请在此处编写相关代码 */




};
int Student::count;
void PrintCount() {
    
    
    cout << "Total " << Student::count << " students" << endl;
}
void Print(Student s) {
    
    
    cout << "the id is " << s.id << endl;
}
int main() {
    
    
    Student::InitCount();
    Student s;
    PrintCount();
    Student s1(10);
    Student s2(s1);
    PrintCount();
    Print(s2);	// 调用拷贝构造函数,调用结束调用析构函数 
    PrintCount();
    return 0;
}

[Description]
Declare and implement a Line class to represent a line segment. The Line class includes:
The private object data members start and end of the Point class represent the two endpoints of the line segment.
A parameterized constructor that sets the segment endpoints to the given parameters.
The member function slope calculates the slope of the line segment.
【Input】
Enter 4 numbers, which represent the coordinates of point 1 (x1, y1) and point 2 (x2, y2).
【Output】
Output the slope of the straight line.
[Input example]
10 20 30 70
[Output example]
2.5
[Hint]
For the Point class, please refer to Experiment 1 in Chapter 5 of "Program Basics - Taking C++ as an Example".
[Source]
Experiment 1 in Chapter 6 of "Basics of Programming - Taking C as an Example".

#include <iostream>
#include <cmath>
using namespace std;


/* 请在此处分别编写Point类和Line类 */
class Line;//向前声明
class Point{
    
    
    
    private:
        double start;
        double end;
    public:
        friend Line;//Line可以访问Point的私有成员了  
        Point(double &x,double &y){
    
    
            start=x;
            end=y;
        }
};

class Line{
    
    
    private:
        double k;
    public:
        Line(const Point &x,const Point &y){
    
    //复制构造函数
            k=(y.end-x.end)/(y.start-x.start);//已知两点,计算斜率
        }

        double slope(){
    
    //输出
            return k;
        }
};

/* 请在此处分别编写Point类和Line类 */


int main() {
    
    
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Point start(x1, y1);
    Point end(x2, y2);
    Line line(start, end);
    cout << line.slope() << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_49868778/article/details/116299122