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

PDF
Javascript foundations: Introducing OO
PPT
ODP
Groovy intro for OUDL
PPTX
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
PPT
JAVA CONCEPTS
PPT
PPTX
Lecture04 polymorphism
Javascript foundations: Introducing OO
Groovy intro for OUDL
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
JAVA CONCEPTS
Lecture04 polymorphism

What's hot (20)

PPT
Ch2 Liang
PDF
CLASSES, STRUCTURE,UNION in C++
PDF
Sql server difference faqs- 9
PDF
Let's make a contract: the art of designing a Java API
DOCX
Advanced data structures using c++ 3
PPTX
PPTX
Introduction to Client-Side Javascript
PPTX
PPT
Lecture21
PDF
C basic questions&amp;ansrs by shiva kumar kella
PPT
Lecture02
PPT
JavaYDL11
PPTX
PPTX
Design patterns with Kotlin
PPTX
11. session 11 functions and objects
PPT
Chapter 2 - Getting Started with Java
PPT
Fast Forward To Scala
PDF
Objectiveccheatsheet
PPTX
C++11 Multithreading - Futures
PDF
Implementation of oop concept in c++
Ch2 Liang
CLASSES, STRUCTURE,UNION in C++
Sql server difference faqs- 9
Let's make a contract: the art of designing a Java API
Advanced data structures using c++ 3
Introduction to Client-Side Javascript
Lecture21
C basic questions&amp;ansrs by shiva kumar kella
Lecture02
JavaYDL11
Design patterns with Kotlin
11. session 11 functions and objects
Chapter 2 - Getting Started with Java
Fast Forward To Scala
Objectiveccheatsheet
C++11 Multithreading - Futures
Implementation of oop concept in c++
Ad

Similar to Brief Summary Of C++ (20)

PPTX
Structured Languages
PPTX
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
TXT
Advance C++notes
PPT
C96e1 session3 c++
PDF
Pointers
PPTX
Chp3(pointers ref)
PPTX
OOPS (object oriented programming) unit 1
PPTX
Chp 3 C++ for newbies, learn fast and earn fast
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
PPT
Data Handling
PPTX
OOC MODULE1.pptx
PPTX
Concept Of C++ Data Types
 
PPTX
class and objects
PDF
polymorphism in c++ with Full Explanation.
PPTX
Introduction to C++
PPTX
C++ - UNIT_-_IV.pptx which contains details about Pointers
PPTX
Unit 1
PPTX
Concept of c data types
Structured Languages
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
Advance C++notes
C96e1 session3 c++
Pointers
Chp3(pointers ref)
OOPS (object oriented programming) unit 1
Chp 3 C++ for newbies, learn fast and earn fast
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Data Handling
OOC MODULE1.pptx
Concept Of C++ Data Types
 
class and objects
polymorphism in c++ with Full Explanation.
Introduction to C++
C++ - UNIT_-_IV.pptx which contains details about Pointers
Unit 1
Concept of c data types
Ad

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PPTX
Cell Structure & Organelles in detailed.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Sports Quiz easy sports quiz sports quiz
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial disease of the cardiovascular and lymphatic systems
human mycosis Human fungal infections are called human mycosis..pptx
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
TR - Agricultural Crops Production NC III.pdf
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
VCE English Exam - Section C Student Revision Booklet
Module 4: Burden of Disease Tutorial Slides S2 2025
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning

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