Enter your Sign-on user name and password.

Forgot password?
  • Follow us on:
Loading video...
Text Comments (9)

0 answers

Post by Moises Tadeo on November 26, 2012

I just signed up and no videos are loading... great!

7 answers

Last reply by: John Nielsen
Tue Dec 18, 2012 2:24 PM

Post by John Nielsen on December 13, 2012

Hello Professor I am having an issue with my pointers that I made. First off these are actually classes that I am pointing to. These classes have the methods below. I want my program to be able to enter say five people objects, then list them. Of course the pointers are giving me trouble. Is this the proper way to implement this type of functionality?



void CreatePerson()
{
Person * people = new Person[ArraySize];
for(int i = 0; i < ArraySize; i++)
{
people[i].setFirstName();
people[i].setLastName();
people[i].setAge();
people[i].setHomePhone();
people[i].setCellPhone();
people[i].setWorkPhone();
people[i].setAddress();
}
delete people;
}


//Here I am trying to pass in the existing people object and display its values

void print(Person * people) //This method displays entered values to the screen
{
for(int i = 0; i < ArraySize; i++)
{
cout << people[i].firstName << endl;
cout << people[i].lastName << endl;
cout << people[i].age << endl;
cout << people[i].homePhone << endl;
cout << people[i].cellPhone << endl;
cout << people[i].workPhone << endl;
cout << people[i].address << endl;
cout << people[i].city << endl;
cout << people[i].state << endl;
cout << people[i].zip << endl;
}
//TODO: Display Entered values to the Screen
}
};

//Here is the case statement in my main function that uses the above functions. This is were I run into problems.


case 1 :
*people.print(people);
break;
case 2 :
*people.CreatePerson();
break;
case 3 :
cout << "Goodbye
";
break;

Structures

  • Structures are a mechanism to better organize data
  • The “struct” syntax, struct { member, member, etc. };
  • Some examples using structures are presented, such as a “person” with a name and an age
  • Members of a structure variable are accessed using the dot (.) operator, e.g. var.member
  • Structures can contain members that are themselves structures
  • Structures can be initialized at compile time, or at run-time
  • Structures can be organized in arrays
  • Even multi-dimensional arrays
  • Pointers can point to a structure,
  • This allows for run-time allocation and de-allocation of structures
  • This is also the only way a structure may have itself as a member
  • Members of a structure pointer are accessed using the (–>) operator, e.g. ptr–>member
  • Here is more information about C++ data structures: http://www.cplusplus.com/doc/tutorial/structures/

Structures

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

Computer Science: Introduction to C++