C++: I/O and Classes: Engineering H192 - Computer Programming
C++: I/O and Classes: Engineering H192 - Computer Programming
Lecture 25
Winter Quarter
Lect 25
P. 1
Both cin and cout can be combined with other member functions for a wide variety of special I/O capabilities in program applications.
Winter Quarter Gateway Engineering Education Coalition Lect 25 P. 3
int main ( ) { int a, b; float k; char name[30]; cout << "Enter your name\n" ; cin >> name ; cout << "Enter two integers and a float\n" ; cin >> a >> b >> k ; cout << "Thank you, " << name << ", you entered\n " ; cout << a << ", " << b << ", and " << k << '\n' ; }
Winter Quarter Gateway Engineering Education Coalition Lect 25 P. 5
Enter your name Rick Enter two integers and a float 20 30 45.67 Thank you, Rick, you entered 20, 30, and 45.67
Winter Quarter
Lect 25
P. 6
Winter Quarter
Lect 25
P. 7
Classes in C++
Classes enable a C++ program to model objects that have: attributes (represented by data members). behaviors or operations (represented by member functions). Types containing data members and member function prototypes are normally defined in a C++ program by using the keyword class.
Winter Quarter
Lect 25
P. 8
Classes in C++
A class definition begins with the keyword class. The body of the class is contained within a set of braces, { } ; (notice the semi-colon). Within the body, the keywords private: and public: specify the access level of the members of the class. Classes default to private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section. Private members of the class are normally not accessible outside the class, i.e., the information is hidden from "clients" outside the class.
Winter Quarter Gateway Engineering Education Coalition Lect 25 P. 9
Classes in C++
A member function prototype which has the very same name as the name of the class may be specified and is called the constructor function. The definition of each member function is "tied" back to the class by using the binary scope resolution operator ( :: ). The operators used to access class members are identical to the operators used to access structure members, e.g., the dot operator (.).
Winter Quarter Gateway Engineering Education Coalition Lect 25 P. 10
Classes Example
#include <iostream> #include <cstring> // This is the same as string.h in C using namespace std; class Numbers // Class definition { public: // Can be accessed by a "client". Numbers ( ) ; // Class "constructor" void display ( ) ; void update ( ) ; private: // Cannot be accessed by "client" char name[30] ; int a ; float b ; };
Winter Quarter Gateway Engineering Education Coalition Lect 25 P. 11
Winter Quarter
Lect 25
P. 13
no1.update ( ) ;
no1.display ( ) ; no2.display ( ) ;
}
Winter Quarter
The name is Rick Freuler The numbers are 9876 and 5.4321 The name is Unknown The numbers are 0 and 0
Winter Quarter
Lect 25
P. 15
Winter Quarter
Lect 25
P. 19
When using these, use the following compile command g++ o g23.out g23.cpp
Winter Quarter
Lect 25
P. 20
Winter Quarter
Lect 25
P. 21
Winter Quarter
Lect 25
P. 22