C++ For Dummies by Stephen R. Davis
Author:Stephen R. Davis
Language: eng
Format: epub, mobi, pdf
Publisher: For Dummies
Chapter 13
Point and Stare at Objects
In This Chapter
Examining the object of arrays of objects
Getting a few pointers on object pointers
Strong typing — getting picky about our pointers
Navigating through lists of objects
C++ programmers are forever generating arrays of things — arrays of ints, arrays of floats — so why not arrays of students? Students stand in line all the time — a lot more than they care to. The concept of Student objects all lined up quietly awaiting their name to jump up to perform some mundane task is just too attractive to pass up.
Declaring Arrays of Objects
Arrays of objects work the same way arrays of simple variables work. (Chapter 7 goes into the care and feeding of arrays of simple — intrinsic — variables, and Chapters 8 and 9 describe simple pointers in detail.) Take, for example, the following snippet from the ArrayOfStudents program:
// ArrayOfStudents - define an array of Student objects
// and access an element in it. This
// program doesn’t do anything
class Student
{
public:
int semesterHours;
float gpa;
float addCourse(int hours, float grade);
};
void someFn()
{
// declare an array of 10 students
Student s[10];
// assign the 5th student a gpa of 4.0 (lucky guy)
s[4].gpa = 4.0;
s[4].semesterHours = 32;
// add another course to the 5th student;
// this time he failed - serves him right
s[4].addCourse(3, 0.0);
}
Here s is an array of Student objects. s[4] refers to the fifth Student object in the array. By extension, s[4].gpa refers to the GPA of the 5th student. Further, s[4].addCourse() adds a course to the 5th Student object.
Declaring Pointers to Objects
Pointers to objects work like pointers to simple types, as you can see in the example program ObjPtr:
// ObjPtr - define and use a pointer to a Student object
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
class Student
{
public:
int semesterHours;
float gpa;
float addCourse(int hours, float grade);
};
int main(int argc, char* pArgs[])
{
// create a Student object
Student s;
s.gpa = 3.0;
// now create a pointer pS to a Student object
Student* pS;
// make pS point to our Student object
pS = &s;
// now output the gpa of the object, once thru
// the variable name and a second time thru pS
cout << “s.gpa = “ << s.gpa << “\n”
<< “pS->gpa = “ << pS->gpa << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system(“PAUSE”);
return 0;
}
The program declares a variable s of type Student. It then goes on to declare a pointer variable pS of type “pointer to a Student object,” also written as Student*. The program initializes the value of one of the data members in s. It then proceeds to assign the address of s to the variable pS. Finally, it refers to the same Student object, first using the object’s name, s, and then using the pointer to the object, pS. I explain the strange notation pS->gpa; in the next section of this chapter.
Dereferencing an object pointer
By analogy of pointers
Download
C++ For Dummies by Stephen R. Davis.mobi
C++ For Dummies by Stephen R. Davis.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12589)
Hello! Python by Anthony Briggs(9926)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9800)
The Mikado Method by Ola Ellnestam Daniel Brolund(9786)
Dependency Injection in .NET by Mark Seemann(9347)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8309)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7771)
Grails in Action by Glen Smith Peter Ledbrook(7705)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7566)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7154)
Microservices with Go by Alexander Shuiskov(6919)
Practical Design Patterns for Java Developers by Miroslav Wengner(6833)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6778)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6423)
Angular Projects - Third Edition by Aristeidis Bampakos(6196)
The Art of Crafting User Stories by The Art of Crafting User Stories(5713)
NetSuite for Consultants - Second Edition by Peter Ries(5644)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5455)
Kotlin in Action by Dmitry Jemerov(5073)
