Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to C++
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Bookmark and Share
Lecture Comments (23)

0 answers

Post by David Amajor on January 6, 2015

this has been soooo helpful, thanks a lot.

3 answers

Last reply by: Alvin Sylvain
Tue Nov 4, 2014 10:27 PM

Post by Ram Manohar Oruganti on October 28, 2014

Hi,
So I have three questions on this lecture:
1. You have introduced us to anonymous classes. So, is the user forced to declare any objects of it at the end of the structure declaration?
2. And when we have recursive pointers in function declaration how would the compiler decide as to how much space a struct needs?
3. Why do we need the braces around the structure pointer when dereferencing it during an attempt to access a struct member?

0 answers

Post by Kitt Parker on July 7, 2014

Had another question about structures.  Thanks for the assistance with the elusive zero char line.  

if I have structure and I try to use this structure with the call

Struct name -> new struct -> string

Test test ("test")


The ("test") would need some kind of constructor, to apply to some variable within the assigned struct?   Thanks again.  ^^

3 answers

Last reply by: Alvin Sylvain
Tue Jun 10, 2014 2:30 PM

Post by Kitt Parker on June 9, 2014

Good Evening Professor,
Perhaps once I have programmed more, it will become more clear.  I'm still unsure why you would point to a value instead of just referencing the value directly.

1 answer

Last reply by: Timothy White
Wed Nov 5, 2014 8:03 AM

Post by Nick White on May 26, 2014

i will be very thankful if you could help me with this question.
A high school has 1000 students and 1000 lockers, one locker for each student. On the first day of school, the principal plays the following game: She asks the first student to go and open all the lockers. She then asks the second student to go and close all the even-numbered lockers. The third student is asked to check every third locker. If it is open, the student closes it; if it is closed, the student opens it. The fourth student is asked to check every fourth locker. If it is open, the student closes it; if it is closed, the student opens it. The remaining students continue this game. In general, the nth student checks every nth locker. If the locker is open, the student closes it; if it is closed, the student opens it. After all the students have taken their turn, some of the lockers are open and some are closed. Write a program that prompts the user to enter the number of lockers in a school. After the game is over, the program outputs the number of lockers that are opened. Test run your program for the following inputs: 1000, 5000, 10000. Do you see any pattern developing?
(Hint: Consider locker number 100. This locker is visited by student numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100. These are the positive divisors of 100. Similarly, locker number 30 is visited by student numbers 1, 2, 3, 5, 6, 10, 15, and 30. Notice that if the number of positive divisors of a locker number is odd, then at the end of the game, the locker is opened. If the number of positive divisors of a locker number is even, then at the end of the game, the locker is closed.)

2 answers

Last reply by: Alvin Sylvain
Tue Feb 11, 2014 8:02 PM

Post by Ram Manohar Oruganti on February 11, 2014

struct Person{
Person *mom;
Person *dad;
};
How come we have points of data type person within person?

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.

  • Intro 0:00
  • Overview 0:10
  • Keep Data Organized 1:25
    • Multiple Arrays
  • Structure Declaration 3:19
    • Example
  • Structure Declaration 4:24
    • Example
  • Structure Usage 6:03
    • Example Structure
  • Accessing Members 8:27
  • Structure Member Structures 9:38
    • Example
  • Initializing Member Structures 11:35
    • Example
    • Access Data Within Data
  • Arrays of Structures 13:11
  • Multi-Dimensional? Of Course! 15:18
  • Pointers to Structures 17:55
  • Pointed-to Structure Members 21:22
  • Structure Pointer Operator 22:41
    • Example
  • Structure for Pointers 24:16