BIG C++ Cay Horstmann & Timothy Budd
Laboratory Notebook Chapter 10 - Pointers
Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
Write a program that creates a vector ring of pointers to Circle objects, that is, a vector<Circle*> ring;. Your job is to fill rings with five pointers to circles that are configured like the interlocking Olympic rings.
Initialize the vector by allocating five circles on the heap with new and calling push_back to append the resulting pointers to the vector.
Write a loop that displays all elements in rings on the graphics screen
Write a loop that deletes all heap objects whose pointers are stored in rings
Combine the code into a main program that prints out the Olympic rings.
/* paste program here */
Note: In this example, there was no advantage to using a vector<Circle*>. It would have been easier to use a vector<Circle>. The purpose of this exercise was to get you used to the pointer notation and dynamic allocation.
This is a worthwhile exercise since the next lab (on inheritance) uses a vector<Card*> to hold a vector of cards (such as an ID card, a phone card, and so on). In that lab, it is essential that you use a vector of pointers--a vector<Card> will not be sufficient.
The following program contains several errors in the way that the pointers are dereferenced. Please correct the program and paste your corrected version in the textarea below.
#include <iostream> #include <string> using namespace std; class Student { public: Student(); Student(string student_name, string major_at_acceptance); void set_name(string new_name); void set_major(string new_major); string get_major() const; string get_name() const; private: string name; string major; }; Student::Student() { name = ""; major = ""; } Student::Student(string student_name, string major_at_acceptance) { name=student_name; major=major_at_acceptance; } void Student::set_name(string new_name) { name = new_name; } void Student::set_major(string new_major) { major = new_major; } string Student::get_major() const { return major; } string Student::get_name() const { return name; } int main(void) { Student* trans_student = new Student("James Smith","Computer Science"); Student* new_student = new Student(); new_student->set_major("Computer Engineering"); cout << (*new_student).get_major() <<"\n"; cout <<"Name: " <<new_student.get_name() <<"\n"; Student next_student = new Student(); *(next_student.set_name("Jack Levenworth")); next_student.set_major("Computer Science"); if (next_student->get_major() == "Computer Engineering") cout <<"Another computer engineer!\n"; return 0; }
Create a class called Student_Club, which will store information on student clubs. The Student_Club class should have as fields President, Vice-President, Secretary, Treasurer, all of which should be pointers to a student object. In this way, a student could hold offices in many different clubs and we would not have to keep separate information about that student in each instance of Student_Club. Your class definition should also include constructors as well as accessor functions.
Rewrite the following program to use pointers instead of arrays:
int main(void) { int salary[20]; int i; for (i=0;i<20;++i) { cout <<"Enter Salary: "; cin >>salary[i]; } for (i=0;i<20;++i) salary[i]=salary[i]+salary[i]/(i+1); return 0; }
Using the Student_Club class created in P3, add one additional field, members, which should be an array of pointers to student objects.
Write a function Count_Cut(char x[]) that accepts a character string as input, counts the number of characters and returns the left half.
Look at the following examples and write your answer in the column next to the example.