CHAP 3 : PENGENALAN KEPADA C++
  Structured Programming in C                     solve-problem (problem)
                    begin
                            if problem is trivial
                            then
                                write solution
                            else
                                breakdown into N subproblems
                                define relationship between subproblem
                                for each subproblem
                                    solve-problem (subproblem)
                    end Pengaturcaraan berasaskan OOP Coding style Benefits of object-based programming style C++ Keywords             asm                    auto                    break                    case                   catch
            char                   class                    const                    continue              default
            delete                 do                      double                  else                     enum
            extern                 float                    for                        friend                  goto
            if                        inline                   int                         long                     new
            operator            private                protected              public                   register
            return                short                    signed                  sizeof                    static
            struct                switch                  template                this                      throw
            try                    typedef                union                    unsigned
            void                  volatile                while                    virtual Streams             #include <iostream.h>            //in C : <stdio.h>
            void main() {
                cout<<"Hello, world\n";     //in C : use printf
            }                 #include <iostream.h>
                void main(){
                    char *courseName = "c++ Programming Course";
                    int grade =99;
                    cout <<"My grade for the "<<courseName;
                    cout <<"is"<<grade<<"\n";
                    // In C:
                    // printf ("My grade for the %s", courseName);
                    // printf ("is %d\n", grade);
                }             #include <iostream.h>
            #define MAX-NAME-LEN 40
            void main() {
                char name [MAX-NAME-LEN];
                int grade;
                cout<<"Enter your name:\n";
                cin >> name;
                cout<<"Enter your grade:\n";
                cin>> grade;
                cout << name << "has a grade of ";
                cout << grade << "\n";
            }
 
Comments Kedudukan penakrifan pembolehubah                 #include <iostream.h>
                #define NUM-STUDENT 20
                void main() {
                    int grade[NUM-STUDENT];
                    for (int i =0; i < NUM-STUDENTS; i++) {
                        char name[40];
                        cout << "Enter your name\n";
                        cin >> name;
                        int grade;
                        cout << "Enter your grade\n";
                        cin >> grades;
                        grade[i] = grades;
                        cout << name << "has a grade of " << grade << "\n"
                    }
                    grade[i] = 100;    // i is NUM-STUDENT + 1 now
                }
 

Constants

                Const int MAX = 10;                    // Const int
                Const char* month = "June";          // Const string
                MAX = 99;                                  // Error
                strcpy (month, "December");         // Error                 int InsertName(Const char* name) {
                    strcpy(rec-name, name);                //legal
                    name[0] = 'a';
                }                const int MAX;                            // Error
 

Enumerations

            enum Size {Small = 1, Medium, Large =5};
            // Small =1, Medium=2, Large =5
            size burgerSize;
            burgerSize = Medium;             enum DesktopItems { clock, ProgManager, Mail};
            enum HouseholdItems {clock, Iron, Fridge}; //Error
 

Function Prototype

                char *GetName (int employeeNum, char *deptCode);  
Function prototype C++ ANSI C
int func(); No parameter Arbitrary no of parameter
int func(void); 
int func(int, int); 
int func(int x, int y); 
int func(.......); 
int func(char*, int, ......);
No. parameters 
2 int 
2 int 
Arbitrary no. 
1 char*, I int, followed by an 
arbitrary no. of parameter
 

Default Function Parameters

            #include <iostream.h>
            void Print(int =1; float=2.3);
            void main() {
                Print();            //use default parameter
                Print(7);          //override first only
                Print(7,8.9)     //override both
            }
 
            void Print(int first, float second){
                cout << "first = " << first << " ";
                cout << "second = " << second << "\n";
            }                 void Print (int=1, float);        // Error
                void Print (int, float = 2.3, long);     //Error                 void Print (int, float=2.3, long=4);
                Print (1);                      // OK, omits second & third
                Print (1,5)                    // Error
 

Inline function

                #include <iostream.h>
                #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
                inline int min (int a, int b) {
                    return ( (a< b) ? a : b);
                }

                void main() {
                    int x,y, z;
                    x=12; y=34;
                    z=MIN(x++, y++);       // side-effect, smaller value
                    cout << "x=" << x << ", y =" << y << "\n";
                    x=12; y=34;                 // work as expected
                    z= min(x++, y++);
                    cout << "x=" << x << ", y =" << y << "\n";
                }

Overloaded functions and operators                 int max(int, int);
                float max(float, float);
                long max(long, long);
                double max(double, double);                 string s1("Hello");
                string s2("world");
                s3 = s1 + s2;                //overload + operator
                cout << s3;

References

                int actualNum;
                int &refNum = actualNum;            //Reference
                int main() {
                    actualNum = 100;
                    cout<< actualNum << "," << refNum<<"\n";
                    actualNum++;
                    cout<< actualNum << "," << refNum<<"\n";
                    refNum = 99;
                    cout << actualNum << "," << refNum << "\n";
                }                 int actualNum;
                int &refNum = actualNum;                 //reference
                int main() {
                    actualNum = 100;
                    //Printing out the address of variable
                    cout << &actualNum << "," << &refNum << "\n";
                }
 

Initialisation of references

            int actualNum;
            int &refNum = actualNum;                //Reference
            int &refNum2;                                   //Error : unitialized References as function parameters Deallocating memory with "delete" Latihan

1. Tulis semula dan lengkapkan aturcara yang terdapat dalam nota ini seperti

  1. aturcara bonus(c++ sahaja)
  2. default function paramater
  3. inline function
  4. reference
  5. reference as function parameter
  6. deallocating memory
2.  Bagi soalan 1. huraikan jawapan(output) yang anda perolehi seolah-olah anda adalah compiler.
 
Hantar latihan anda sebelum 22 Dis 1998.