chap1 - Chapter 1 Structs and Classes 1 Structs A struct...

Info iconThis preview shows pages 1–26. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 1 Structs and Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Structs A struct holds data, like an array Each unit of data in a struct is called a data member (or member ) they are called “elements” in arrays In a struct, each data member can have a different data type in arrays, the data type of each element is the same
Background image of page 2
3 Example Using a Struct 1 #include <iostream> 2 #include <iomanip> 3 #include <string> 4 5 using namespace std; 6 7 struct CarType { 8 string maker; 9 int year; 10 float price; 11 }; 12 13 void getYourCar( CarType & car ); 14 Don’t forget this semicolon.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 15 int main( ) 16 { 17 CarType myCar, yourCar; 18 19 myCar.maker = "Mercedes"; // I wish 20 myCar.year = 2005; 21 myCar.price = 45567.75; 22 Example Using a Struct (cont.)
Background image of page 4
5 23 getYourCar( yourCar ); 24 25 cout << "Your car is a: " << yourCar.maker << endl; 26 cout << fixed << showpoint << setprecision( 2 ) << 27 "I'll offer $" << yourCar.price - 100 << 28 " for your car." << endl; 29 30 return 0; 31 } 32 Example Using a Struct (cont.)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 33 void getYourCar( CarType & car ) 34 { 35 cout << "Enter your maker: "; 36 cin >> car.maker; 37 cout << "Enter the year: "; 38 cin >> car.year; 39 cout << "Enter the price: $"; 40 cin >> car.price; 41 } Example Using a Struct (cont.)
Background image of page 6
7 Object Assignment An object of a struct can be assigned to another object of the same struct type: myCar = yourCar; This assigns each data member in yourCar to the corresponding data member of myCar Also assigns any array data members
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Classes A class is similar to a struct A class contains data members, but it also contains function members Objects are made from classes, similarly to the way that objects are made from structs The main program communicates with the objects data is passed from main program to object and from object back to the main program
Background image of page 8
9 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 10
11 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 12
13 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 14
15 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 16
17 Main Program Using Objects Object A Object B Object C Main Program
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
18 How the Main Program Uses A Class Object The main program does not access the data within a class object The main program only accesses the functions of a class object communication occurs by passing data as parameters into the object’s function the object passes data to the main program through its return type
Background image of page 18
19 Main Program and Object Main Progra m Object public: functio ns private: data
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
20 Main Program Calls a Function in the Object Main Progra m Object public: functio ns private: data
Background image of page 20
21 The Function Accesses Data Main Progra m Object public: functio ns private: data
Background image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
22 Function Returns a Value Back to the Main Program Main Progra m Object public: functio ns private: data
Background image of page 22
23 Main Program Calls a Different Function Main Progra m Object public: functio ns private: data
Background image of page 23

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
24 Function Calls Another Function Main Progra m Object public: functio ns private: data
Background image of page 24
25 Second Function Accesses Data Main Progra m Object public: functio ns private: data
Background image of page 25

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 26
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/29/2008 for the course CS 310 taught by Professor Dr.panja during the Spring '08 term at Morehead State.

Page1 / 120

chap1 - Chapter 1 Structs and Classes 1 Structs A struct...

This preview shows document pages 1 - 26. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online