Lecture 25 - C++ IO&Classes - 06

Embed Size (px)

Citation preview

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    1/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 1Winter Quarter

    C++: I/O and Classes

    Lecture 25

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    2/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 2Winter Quarter

    Keyboard & Screen I/O in C++

    It is perfectly valid to use the same I/O statements inC++ as in C -- The very same pr int f, scanf, and otherstdio.hfunctions that have been used until now.

    However, C++ provides an alternative with the newstream input/output features. The header file isnamed iostream and the stream I/O capabilities areaccessible when you use the pre-processordeclaration:

    #include // No .h on std headersusing namespace std; // To avo id th ings like

    // s td ::cou t and s td ::c in

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    3/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 3Winter Quarter

    Keyboard & Screen I/O in C++

    Several new I/O objects available when you

    include the iostream header file. Two important

    ones are:

    cin // Used for keyboard input (std::cin)

    cout // Used for screen output (std::cout)

    Both c inand coutcan be combined with other

    member functions for a wide variety of special I/O

    capabilities in program applications.

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    4/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 4Winter Quarter

    Keyboard & Screen I/O in C++

    Since cin and cout are C++ objects, they aresomewhat "intelligent":

    They do not require the usual format strings

    and conversion specifications. They do automatically know what data types

    are involved.

    They do not need the address operator, &.

    They do require the use of the streamextraction ( >> ) and insertion (

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    5/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 5Winter Quarter

    Example using cin and cout#include using namespace std; // replace every cin and cout

    // with std::cin and std::cout

    // without this line

    int main ( )

    {int a, b; float k; charname[30];

    cout> name ;

    cout > a >> b >> k ;

    cout

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    6/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 6Winter Quarter

    Example Program Output

    Enter your name

    Rick

    Enter two integers and a float

    20 30 45.67

    Thank you, Rick, you entered

    20, 30, and 45.67

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    7/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 7Winter Quarter

    Input Stream Object Member Functions

    cin.getline(array_name, max_size);

    Example:charname[40] ;

    cin.getline(name, 40); // gets a string from

    // keyboard and assigns

    // to name

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    8/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 8Winter Quarter

    Classes in C++

    Classes enable a C++ program to model objects

    that have:

    attributes (represented by data members).

    behaviors or operations (represented bymember funct ions) .

    Types containing data membersand member

    funct ionprototypes are normally defined in a C++program by using the keyword class.

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    9/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 9Winter Quarter

    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

    publ ic :specify the access level of the membersof 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 publ ic :section. Private members of the class are normally not

    accessible outside the class, i.e., the information

    is hidden from "clients" outside the class.

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    10/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 10Winter Quarter

    Classes in C++

    A member function prototype which has the verysame name as the name of the class may bespecified and is called the constructor function.

    The definition of each member function is "tied"back to the class by using the binary scoperesolution operator ( :: ).

    The operators used to access class members areidentical to the operators used to accessstructure members, e.g., the dot operator (.).

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    11/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 11Winter Quarter

    Classes Example

    #include #include // 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"

    charname[30] ;int a ;

    float b ;

    } ;

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    12/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 12Winter Quarter

    Classes Example (continued)

    Numbers::Numbers ( ) // Constructor member function{

    strcpy(name, "Unknown");

    a = 0;

    b = 0.0;}

    void Numbers::display ( ) // Member function

    {

    cout

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    13/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 13Winter Quarter

    Classes Example (continued)

    void Numbers::update ( ) // Member function

    {

    cout

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    14/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 14Winter Quarter

    Classes Example (continued)

    int main ( ) // Main program

    {

    Numbers no1, no2 ; // Create two objects of

    // the class "Numbers"

    no1.update ( ) ; // Update the values of

    // the data members

    no1.display ( ) ; // Display the current

    no2.display ( ) ; // values of the objects

    }

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    15/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 15Winter Quarter

    Example Program Output

    r1tel (~) freuler 53> example.out

    The name is Rick FreulerThe numbers are 9876 and 5.4321

    The name is Unknown

    The numbers are 0 and 0

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    16/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 16Winter Quarter

    More Detailed Classes Example

    #include #include

    using namespace std;

    class Numbers // Class definition

    {

    public:Numbers (char[ ] = "Unknown", int = 0, float = 0.0);

    void display ( );

    void update ( ) ;

    private:

    charname[30];int a;

    float b;

    } ;

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    17/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 17Winter Quarter

    More Detailed Classes Example (continued)

    Numbers::Numbers (charnm[ ], int j, float k )

    {

    strcpy(name, nm);

    a = j ;

    b = k ;

    }

    void Numbers::update ( )

    {

    cout > b ;

    }

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    18/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 18Winter Quarter

    More Detailed Classes Example (continued)

    void Numbers::display( ){

    cout

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    19/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 19Winter Quarter

    More Detailed Example Program Output

    r1rhl (~) freuler 76> example.out

    The name is Unknown

    The numbers are 0 and 0

    The name is John Demel

    The numbers are 12345 and 678.9

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    20/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 20Winter Quarter

    Some Help for G23

    Use

    #include

    using namespace std;

    When using these, use the following compile

    command

    g++ o g23.out g23.cpp

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    21/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 21Winter Quarter

    Some Guidance on G23

    class Complex

    { private:

    float fReal;

    float fImag;

    public:

    Complex(); // Constructor function prototype

    void mult(_______); // rest of function prototypes};

    What goes here?

  • 7/29/2019 Lecture 25 - C++ IO&Classes - 06

    22/22

    Engineering H192 - Computer Programming

    Gateway Engineering Education Coalition Lect 25 P. 22

    http://www.google.com.ph/search?q=introduction+to+c%2B%2B.ppt&ie=utf-8&oe=utf-

    8&aq=t&rls=org.mozilla:en-US:official&client=firefox-

    a#q=introduction+to+c%2B%2B.ppt&hl=fil&client=firefox-a&hs=uIl&rls=org.mozilla:en-

    US:official&prmd=imvns&ei=IIgjUMOqMIOPmQWPxYDwDA&start=10&sa=N&bav=on.2,or.r_gc.r_pw.r_

    qf.&fp=bc18a79a89ac87b8&biw=1291&bih=693

    Hints on Multiplying Complex Numbers

    For the function

    void Complex::mult (Complex imag_n)

    {

    // you need to use the distributive property!

    // (a + b*i)*(c + d*i) = (a*c b*d) + (b*c + a*d)*i

    // real part + iimag part

    }