SlideShare a Scribd company logo
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
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
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
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
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

More Related Content

What's hot

CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
Prof Ansari
 
Sql server difference faqs- 9
Sql server difference faqs- 9Sql server difference faqs- 9
Sql server difference faqs- 9
Umar Ali
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
Mario Fusco
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3Shaili Choudhary
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
Chapter 2
Chapter 2Chapter 2
C basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kellaC basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kella
Manoj Kumar kothagulla
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with Kotlin
Alexey Soshin
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 
Fast Forward To Scala
Fast Forward To ScalaFast Forward To Scala
Fast Forward To Scala
Martin Kneissl
 
Objectiveccheatsheet
ObjectiveccheatsheetObjectiveccheatsheet
Objectiveccheatsheetiderdelzo
 
C++11 Multithreading - Futures
C++11 Multithreading - FuturesC++11 Multithreading - Futures
C++11 Multithreading - Futures
GlobalLogic Ukraine
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 

What's hot (20)

Ch2 Liang
Ch2 LiangCh2 Liang
Ch2 Liang
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
Sql server difference faqs- 9
Sql server difference faqs- 9Sql server difference faqs- 9
Sql server difference faqs- 9
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Lecture21
Lecture21Lecture21
Lecture21
 
C basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kellaC basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kella
 
Lecture02
Lecture02Lecture02
Lecture02
 
JavaYDL11
JavaYDL11JavaYDL11
JavaYDL11
 
ccc
cccccc
ccc
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with Kotlin
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
 
Fast Forward To Scala
Fast Forward To ScalaFast Forward To Scala
Fast Forward To Scala
 
Objectiveccheatsheet
ObjectiveccheatsheetObjectiveccheatsheet
Objectiveccheatsheet
 
C++11 Multithreading - Futures
C++11 Multithreading - FuturesC++11 Multithreading - Futures
C++11 Multithreading - Futures
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 

Similar to Brief Summary Of C++

Lecture 8
Lecture 8Lecture 8
Lecture 8
Mohammed Saleh
 
Link list
Link listLink list
Link list
Malainine Zaid
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Mohammed Saleh
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
Java Concepts
Java ConceptsJava Concepts
Java Concepts
AbdulImrankhan7
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptxObject Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
RashidFaridChishti
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
rattaj
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
HimanshuSharma997566
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
ishan743441
 

Similar to Brief Summary Of C++ (20)

Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Link list
Link listLink list
Link list
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Java Concepts
Java ConceptsJava Concepts
Java Concepts
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Overloading
OverloadingOverloading
Overloading
 
Lecture5
Lecture5Lecture5
Lecture5
 
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptxObject Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 

Recently uploaded

How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 

Recently uploaded (20)

How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 

Brief Summary Of C++

  • 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. 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. 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. 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. 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