انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

BEGINNING WITH C PlusPlus

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة مهدي عبد سلمان المسلماوي       3/29/2011 9:45:45 AM

BEGINNING WITH C++

 

What is C++?

 

C++ is an object-oriented programming language. C++ is a superset of C. Most of what we already know about C applies to C++ also.

The three most important facilities that C++ adds on to C are Classes, Function Overloading, and operator overloading. These features enable us to create abstract data type, inherit properties from existing data types and support polymorphism. Thus making C++ a truly object-oriented language.

The object-oriented features in C++ allow programmers to build large programs with clarity, extensibility and ease of malignance, incorporating the sprint and efficiency of C.

A simple C++ program

 

Let us begin with a simple example of a C++ program that print string to screen.

 

/////////////////////////// [ printing a string ] ////////////////////////

 

# include < iostream.h> // include header file

 

 

main()

 

{

 

            cout << " c++ is  better. " ; // C++ statement

 

}

 

 

// end of example

 

/////////////////////////// [ program 1] ////////////////////////////

 

 

 

Program feature

 

-          The C++ program is a collection of functions.

-          The example contain only one function main( ).

-          Execution begins at main( ).

-          Every C++ program must have a main( ).

-          The C++ statements terminate with semicolons.

Comments

 

Comments start with double slash (//) symbol and terminate at the end of line.

Multiline comments can be written as follows:

                        /* this is an example of

 

                           C++ program to illustrate

 

                           Some of its features

 

                        */

 

Output Operator

 

The statement

                        cout << "C++ is better C.";

 

causes the string in quotation marks to be displayed on screen.

This statement introduces two new C++ features, cout and <<. The identifier cout is a predefined object that represents the standard output stream in C++. The operator << is called the insertion or put to operator.

 

The iostream.h file

 

We have used the following # include directive in the program:

                        <#include iostream.h>

 

This directive causes the preprocessor to add the contents of th iostream.h file to the program. It contains declarations for the identifier cout and operator <<.

The header file iostream.h should be included at the beginning of all programs that use input/output statements.

 

Return statement

 

C++ main( ) return an integer type value to the operating system. Therefore, every main( ) in C++ should end with a return(0) statement; otherwise a warning or an error might occur.

/////////////////////[AVERAGE OF TWO NUMBERS]//////////////

 

# include <iostream.h>

 

 

main ( )

 

{

 

    Float number1, number2,

 

             Sum, average;

 

    cout << "Enter two numbers : "; // prompt

 

    cin   >>  Number1;                    // Reads numbers

 

    cin   >>  Number2;                    // from key board

 

   

 

     sum=number1 + number2;

 

     average=sum/2;

 

 

     cout << "sum = " <<sum << "\n";

 

     cout << "Average = " << average << "\n";

 

}

 

///////////////////////[PROGRAM 2.2 ]///////////////////////////

 

The output of program 2.2 is :

                        Enter two numbers : 6.5 7.5

                        Sum = 14

                        Average = 7

Input Operator

 

The statement

                                    cin >> number1;

is an input statement and causes the program to wait for user to type in a number.

 

Cascading of I/O Operators

 

We have used extraction operator << repeatedly in the last two statements for printing results. The statement

                                                cout << "Sum = " << sum << "\n";

 

firstsends the string "Sum = " to cout  and then sends the value of sum. Finally, it sends the newline character so that the next output will be in the new line.

 

We can also cascade input operator >> as shown below:

                                    cin >> number1 >> number2;


SAMPLE PROGRAM WITH CLASS

 

One of the major features of C++ is classes. They provide a method of binding together data and functions which operate on them. Classes are user-defined data types.

 

////////////////////////////[ USE OF CLASS ] ////////////////////////////

 

//-----------------------------------------include section ---------------------------------------------------------

 

#include <iostream.h>

 

 

 

 

//-----------------------------------------     Class Declaration ------------------------------------------------------

 

Class person

 

{

 

          char name[30];

 

           int age;

 

           public:

 

                     void getdata(void);

 

             void display(void);

 

};

 

//-------------------------------------------- Member functions declaration ----------------------------------------

 

Void person : : getdata(void)                  // member function

 

{

 

        cout << "Enter name: ";

 

         cin  >> name;

 

        cout << " Enter age : ";

 

         cin  >> age;

 

}

 

Void display : : display(void)                    // member function

 

{

 

        cout << "\n Nmae: " << name;

 

        cout << "\n Age   : " << age;

 

}

 

//-------------------------------------------- Main section ( use for test ) -------------------------------------------

 

main ( )

 

{

 

        person p;                                           // Object of type person

 

        p.getdata();

 

        p.display();

 

}

 

///////////////////////////// [ program 2.3] //////////////////////////////

 

 The output will be

            Enter Name: Fatima

            Enter Age   :20

 

            Name: Fatima

            Age:    20

- The program defines person as a new data of type class.

- The class person includes two basic data type items and two functions to operate on that data.

- These functions are called member functions.

- The main program uses person to declare variables of type person.

- Class variables are known as objects. Here p  is an object of type person.

 

STRUCTURE OF C++ PROGRAM

A typical C++ program would contain four sections as shown in figure bellow. These sections may place in separate code files and then compiled independently or jointly.

 

 

Include files

 

 

Class declaration

 

 

Class functions definitions

 

 

Main function program

 

 

   

 

It is a common practice to organize a program into three separate files. The class declarations are placed in a header file and the definitions of member functions go into another file. Finally, the main program that uses the class is placed in a third file which " includes" the previous two files as well as any other required.

This approach is based on the concept of client-server model as shown in figure below. The class definitions including the member functions constitute the server that provides services to the main program known as client. The client uses the server through the public interface of the class.

 

 

Member functions

Classes definition

Main function program

  

Server

Client

Questions:

1- Why do we need the preprocessor directive #include <iostream.h>?

2- Describe the major parts of C++ program.

3- Find errors, if any in the following C++ statements.

    (a) cout <<"x="x;

    (b) m=5;//n=10;//s-m+n;

    (c) cin >>x;>>y;

   (d) cout <<\n "Name :" << name;

   (e) cout <<"Enter value:";cin>>x;

   (f) /* Addition */ z=x-y;


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .