Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++: Two Examples of Structured Data #211

Open Qingquan-Li opened 2 years ago

Qingquan-Li commented 2 years ago

Concept: Abstract data types (ADTs) are data types created by the programmer. ADTs have their own range (or domain) of data and their own sets of operations that may be performed on them.

Example01:

#include <iostream>

using namespace std;

// Global variable:
struct Employee {
    string first_name;
    string last_name;
    int id;
    int year_of_birth;
    double salary;
};

// Function prototype:
void printEmployeeInfo(const Employee&);

int main() {
    const int NUM_TEAM_MEMBERS = 10;
    Employee bestEmployee;
    Employee anotherEmployee = {"Jane", "Smith", 3456, 1992, 89000};
    Employee team[NUM_TEAM_MEMBERS];

    Employee* eptr = &bestEmployee;

    bestEmployee.first_name = "John";
    bestEmployee.last_name = "Doe";

    cout << "The best employee is " << bestEmployee.first_name << " " << bestEmployee.last_name << endl;
    cout << endl;

    /*
    cout << "Please type in an employee ID for the employee: ";
    cin >> bestEmployee.id;
    cout << "Please type in the birth year for the employee: ";
    cin >> bestEmployee.year_of_birth;
    cout << "Please type in a salary for the employee: ";
    cin >> bestEmployee.salary;

    cout << "EmployeeID: " << bestEmployee.id << " is "
         << (2022 - bestEmployee.year_of_birth) << " years old. "
         << "And makes $" << bestEmployee.salary << " annually." << endl;
    */

    // (*eptr).id = 1111;
    // (*eptr).year_of_birth = 2000;
    // (*eptr).salary = 56000;
    eptr->id = 1111;
    eptr->year_of_birth = 2000;
    (*eptr).salary = 56000;

    cout << "The best employee's info:" << endl;
    printEmployeeInfo(bestEmployee);
    cout << endl;
    cout << "The another employee's info:" << endl;
    printEmployeeInfo(anotherEmployee);
    cout << endl;

    team[0].first_name = "Victor";
    team[0].last_name = "D";
    team[0].id = 1356;
    team[0].year_of_birth = 1972;
    team[0].salary = 99999;
    cout << "Our first team member's info: " << endl;
    printEmployeeInfo(team[0]);

    return 0;
}

void printEmployeeInfo(const Employee& emp) {
    cout << "Employee ID: " << emp.id << endl;
    cout << "Employee Name: " << emp.first_name << " " << emp.last_name << endl;
    cout << "Employee Age: " << (2022 - emp.year_of_birth) << endl;
    cout << "Employee Salary: " << emp.salary << endl;
}

Output:

The best employee is John Doe

The best employee's info:
Employee ID: 1111
Employee Name: John Doe
Employee Age: 22
Employee Salary: 56000

The another employee's info:
Employee ID: 3456
Employee Name: Jane Smith
Employee Age: 30
Employee Salary: 89000

Our first team member's info: 
Employee ID: 1356
Employee Name: Victor D
Employee Age: 50
Employee Salary: 99999

Example02:

// This program asks the user to input a student's GPA and then prints the highest-ranked,
// and the second-highest ranked student's information by comparing their GPA.

#include <iostream>
#include <string>

using namespace std;

// Globally define a struct with the tag Student
struct Student {
    int student_id;
    string first_name;
    string last_name;
    float gpa;
};

// Function prototype
void print_student_info(const Student&);

int main() {
    Student cis_student = {1234, "Tim", "Berners-Lee"};
    Student csc_student = {2345, "Barbara", "Liskov", 3.99};

    // Declare a pointer to a Student and name the pointer valedictorian (the highest ranked student).
    // Declare a second pointer to a Student and name the pointer salutatorian (the second-highest ranked student).
    Student* valedictorian = nullptr;
    Student* salutatorian = nullptr;

    // Ask the user for the gpa for the CIS student, Tim Berners-Lee, and have the value assigned to his gpa.
    cout << "What is " << cis_student.first_name << " " << cis_student.last_name << "'s gpa? ";
    cin >> cis_student.gpa;

    cout << endl;

    // The one with the highest GPA will be the valedictorian. (Assume they will not have the same gpa.)
    // The if statement will assign the address of the Student with the higher gpa to the valedictorian pointer
    // and assign the address of the other Student to the salutatorian pointer.
    if (cis_student.gpa > csc_student.gpa) {
        valedictorian = &cis_student;
        salutatorian = &csc_student;
        // print the valedictorian's info by dereferencing the valedictorian pointer.
        // Print the salutatorian's info by dereferencing the salutatorian's pointer.
        cout << "The valedictorian's info: \n";
        print_student_info(*valedictorian);
        cout << endl;
        cout << "The salutatorian's info: \n";
        print_student_info(*salutatorian);
    } else {
        valedictorian = &csc_student;
        salutatorian = &cis_student;
        cout << "The valedictorian's info: \n";
        print_student_info(*valedictorian);
        cout << endl;
        cout << "The salutatorian's info: \n";
        print_student_info(*salutatorian);
    }

    return 0;
}

void print_student_info(const Student& stu) {
    cout << "Student ID: " << stu.student_id << endl;
    cout << "Student Name: " << stu.first_name << " " << stu.last_name << endl;
    cout << "Student gpa: " << stu.gpa << endl;
}

Output:

What is Tim Berners-Lee's gpa? 3.98

The valedictorian's info: 
Student ID: 2345
Student Name: Barbara Liskov
Student gpa: 3.99

The salutatorian's info: 
Student ID: 1234
Student Name: Tim Berners-Lee
Student gpa: 3.98