Brief Summary Of C++


Published on

C++ Notes

Published in: Education, Technology, Business
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Brief Summary Of C++

  1. 1. Brief Summary of C++ (Mainly on Class) Attributes of OOP 1) Encapsulation a. Hide the data (make the data variable private) and hide the internal function that is not needed by the external user b. Only exposes the functions that are required by the external user. User calls these public functions in order to use the object. 2) Code reuse a. Use inheritance to reuse existing code 3) Abstraction Use class to model the things you have in your program. Program consist of objects that talk to each other by calling each other public member function. 4) Generalization a. Use template class or template function to build generic class or function b. Generic function can process different type of data c. Build a general class and use it as a base to derive specialized class (inheritance) Variable - Refers to memory location used to store data - Is given a name example weight, myID . It is not convenient to memorize memory address e.g A1230000FFA Variable data type can be classified to a few types a) Simple data type int, char , float, double int weight = 0; // declare and initialize the variable in 1 line of code. b) Array Array is a compound data type whereby it stores a group of data int weightArray[5] ; // declare an array called weightArray to store 5 integer type data int weightArray[5] ; // declare an array called weightArray to store up to 5 integer type data int weightArray2[5] = {1,2,3,4,5}; // You can also declare and initialize cout << endl << weightArray2[2] ; //% display the 3rd element c) Pointer Pointer is a type of variable that is used to represent other variable It is mainly used to represent an array, object and also to pass data to a function by reference. In addition the function can pass data to the pointer so that it can be retrieved by the caller function. Pointer stores the address of the variable that it points to. 1
  2. 2. int* ptr ; // declare a pointer to int and name the pointer as ptr int* ptr2 = &weight ; // declare a pointer to int and initialize it to point to the variable weight in 1 line of code ptr = &weight2 ; // The previously declared pointer is now pointed at the variable weight2 cout << endl << *ptr2 << endl ; // display the data in the variable pointed by ptr2, display 0 since weight=0 Using pointer to represent array int* arrayPtr; arrayPtr = weightArray2 ; //arrayPtr now points to the array // weightArray2 // take note that the array name hold the address of the array cout << endl << arrayPtr ; // display the address of the 1st element cout << endl << arrayPtr+1 ; // display the address of the 2nd element cout << endl << *(arrayPtr) ; // display 1 cout << endl << *(arrayPtr+1) ; // display 2 cout << endl << arrayPtr[0] ; // display 1 cout << endl << arrayPtr[1] ; // display 2 char* mystring = "Hello" ; cout << mystring; const int* pt = weightArray ; // pt is a pointer to a constant, you cannot use pt to change the value of weightArray // *(pt+1) = 200 ; this is wrong int* const pt2 = weightArray; // pt2 is a constant pointer, you cannot use pt2 to point to other variable, pt2 always represents weightArray // pt2 = weightArray2 , this is wrong d) Reference Reference is a variable that represent other variable as an alias. It is mainly used to pass data to a function by reference. In addition the function can pass data to the reference variable so that it can be retrieved by the caller function. int& rnum = weight2; // declare a reference variable by the name rnum and initialize it to represent weight2 // reference variable must always be initialized when it is first declared cout << endl << rnum ; // display 10, the value of weight2 rnum = weight ; // reassign rnum to represent the variable weight cout << endl << rnum ; // display 0, the value of weight e) Class and Structure In C++ there are many predefined variable type such as int, char, float However you can also create your own data type using the class and struct construct. 2
  3. 3. Class is a c++ language construct that is used as a blueprint to create objects Object is an instance of the class and is a variable. Object contain member function and member variable. Member function provide the service and member variable store data. Member can be private , public, protected // Declare a class by the name of Student // the purpose or responsibility of Student class is to store students information // To fulfill its responsibility , it provides a set of services to the user // User can invoke the public member function for example getCgpa() to retrieve the cgpa value stored in the object class Student { public: Student(); // default constructor Student(float p_cgpa) { _cgpa = p_cgpa ;} // parameterized constructor Student(string name); Student(string name, string address); float getCgpa(); // member function void set_CgpaAndAge( float p_cgpa, int p_age) { _cgpa = p_cgpa ; _age = p_age; } void setName(string pName) { _name = pName;} string getAddress(); void getcgpaAndAge(float& p_cgpa, int& p_age); private: string _name; string _address; char _icNumber[20]; float _cgpa; int _age; } ; // Test program to test the class and its member function defined above int main() { Student stud1("Lim"); // Create the object called stud1 and initialize its name stud1.set_CgpaAndAge(3.99,20); // set the cgpa value and age of object stud1 } Getting function to return more than one value Suppose in the main function we want to retrieve the cgpa and age value of the object, we cannot write a function to return 2 values. A function can only return 1 value. One way to solve this problem so that a function can pass more than 1 3
  4. 4. variable back to the caller function is to use the reference variable as the function argument. main( ) { int x=0, y=0; functionName(x,y) ; cout << x << y ; // Display 100 200 } void functionName (int& arg1, int& arg2) { arg1 = 100; arg2 = 200; } This way the function functionName can return the 2 integer values 100, 200 back to the main function. By using reference variable you can pass multiple data to a function and also use it as container to store the data that need to be returned by the function. Here is the implementation for the Student class member function The main () function need to retrieve the cgpa value and the age value from the Student object stud1. 1) Method 1 : Use a reference variable // the function below retrieve the data _cgpa and _age from the Student object and pass them to the reference variable so that the caller function can get hold of this data void Student::getcgpaAndAge(float& p_cgpa, int& p_age) { p_cgpa = _cgpa ; p_age = _age ; // assign member variable to argument // variable } int main() { int age; float cgpa; Student stud1("Lim"); // Create the object called stud1 and initialize its name stud1.set_CgpaAndAge(3.99,20); // set the cgpa value and age of object stud1 stud1.getcgpaAndAge(cgpa, age); // request the stud1 object to get and return its cgpa value and age value cout << endl << "cgpa = " << cgpa; // display 3.99 cout << endl << " age = " << age; } 4
  5. 5. Program output Output of object s4 Ali Output of object s4 access using pointer Ali Output of object s4 access using reference Ali Output of object ecpStudents[1] ecpStudents[1]._name = Ah Kow Output of object ecpStudents[2] in the array , this time the array is represented using the pointer pStd ecpStudents[2]._name = pStd[2]._name = Aminah Use pointer pStd2 to represent the array of 3 Student objects, access the 2nd object pStd2[1]._name = No name yet This program allow the Student object to be added to the Subject object Subject object has many Student object Constructor called: Subject Name: ECP4206 OOP Programming Number of Student are 2 Phua CK Obama 5
  6. 6. 6