Previous Lecture lect10 Next Lecture

lect10, Thu 05/02

Structs

Helpful online visualization of C++ programs (may help to understand pointers): [http://pythontutor.com/cpp.html#mode=edit]

Structs

Diagrams

Code from lecture

//print_student.cpp

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

struct Student {
    string fname;
    string lname;
};

void print_student( Student s);
// Precondition: Student s has been initialized
// Postcondition: Student information is printed
// one value on each line

int main()
{
    Student student1;
    Student student2;
    Student student3;
    Student &s_ref = student2;

    student1.fname = "Hermione";
    student1.lname = "Granger";
    student2.fname = "Harry";
    student2.lname = "Potter";

    //cout << student1.fname << endl;
    //cout << student1.lname << endl;
    print_student(student1);

    cout << s_ref.fname << endl;
    s_ref.fname = "Ron";
    cout << s_ref.fname << endl;

    print_student(student2);


    return 0;
}

void print_student( Student s)
// Precondition: Student s has been initialized
// Postcondition: Student information is printed
// one value on each line
{
    cout << s.fname << endl;
    cout << s.lname << endl;
}

Pass the struct by reference to set its variables

// init_student.cpp


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

struct Student {
    string fname;
    string lname;
};

void print_student( Student s);
// Precondition: Student s has been initialized
// Postcondition: Student information is printed
// one value on each line

void init_student(Student &s, string first, string last);
// Precondition: Student s is declared
// Postcondition: s.fname is set to "first", last name is assigned to s.lname

int main()
{
    Student student1;
    Student student2;
    Student student3;
    Student &s_ref = student2;

    /*
    student1.fname = "Hermione";
    student1.lname = "Granger";
    student2.fname = "Harry";
    student2.lname = "Potter";
    */
    init_student(student1, "Hermione", "Grager");
    init_student(student2, "Harry", "Potter");

    //cout << student1.fname << endl;
    //cout << student1.lname << endl;
    cout << "from main" << endl;
    print_student(student1);
   /*
    cout << s_ref.fname << endl;
    s_ref.fname = "Ron";
    cout << s_ref.fname << endl;
    */
    print_student(student2);


    return 0;
}

void print_student( Student s)
// Precondition: Student s has been initialized
// Postcondition: Student information is printed
// one value on each line
{
    cout << s.fname << endl;
    cout << s.lname << endl;
}

void init_student(Student &s, string first, string last)
// Precondition: Student s is declared
// Postcondition: s.fname is set to "first", last name is assigned to s.lname
{
    s.fname = first;
    s.lname = last;

    cout << "from init_student" << endl;
    print_student(s);
    return;
}

Partial example with coordinates

#include <iostream>
using namespace std;

struct Coord {
    int x;
    int y;
};

struct Box {
    int width;
    int height;
    Coord upper_left;
};

struct BigBox {
    int bb_width;
    int bb_height;
    Box inner_box;
    Box outer_box;
};

int main()
{

    Coord coord1 = {4,3};

    Box b1 = {22, 33, coord1};
    Box b2 = {44, 55, {1,7}};

    BigBox bb;

    b1.width = 2;
    b1.height = 3;
    b1.upper_left.x = 1;
    b1.upper_left.y = 2;

    return 0;
}

Practice Questions

  1. Write the function modifyCourse so that the following program outputs CS 16 5 John Doe ``` struct Student { string firstName; string lastName; };

struct Course { string courseName; int unit; Student s1; };

int main() { Course c1; c1.unit = 5; c1.courseName = “CS 8”; modifyCourse(c1, “CS16”, 4, “John”, “Doe”); cout « c2.courseName « ” “ « c2.unit « ” “ « c1.s1.firstName « ” “ « c1.s1.lastName « endl; return 0; } ```

  1. Design a struct to represent a club at UCSB. Assume that every club needs a name, bank account, and a student president; you are allowed to use any structs defined in the previous question.