CPP Notes

C
DSA
Software Engineering
Software Architecture
Operating System
Big Data
Data Mining and Warehousing
TOC
Ada
CPP
DBMS

All Topics (10)

  • 1. Introduction to Cpp
  • 2. Why Cpp is Called Cpp
  • 3. Why Cpp is Used
  • 4. Difference Between C and Cpp (Syntax)
  • 5. What is Cpp.
  • 6. What are private and public in Cpp?
  • 7. General Syntax of Declaring a Class
  • 8. Cpp Code
  • 9. Program.
  • 10. What is Relation between OOP and Cpp

6. What are private and public in Cpp?

  • private and public are keywords.

  • These keywords are popularly called access specifiers or visibility modes.

  • They control which members of a class are accessible from outside the class and which are not.

  • Members declared as public can be accessed from outside the class.

  • Members declared as private cannot be accessed from outside the class.

  • Normally, we declare data members of a class as private and member functions as public.

  • In C++, when we design a class, everything is private by default, so there is no need to explicitly write private.

  • To make members accessible from outside, we use the keyword public.

  • There is one more keyword called protected, which is also an access specifier, but it is discussed only in the chapter on Inheritance.

7. General Syntax of Declaring a Class

Syntax 1:

class ClassName
{
// Data members
public:
// Member functions
};
 

Syntax 2:

class ClassName
{
public:
// Member functions
private:
// Data members
};
 

Notes:

  • public → member functions accessible from outside the class.

  • private → data members not accessible from outside.

8. Cpp Code

#include <iostream>
using namespace std;

class Student {
private:
    int roll;  // private data member
    char grade;  // private data member
    float per;  // private data member

public:
    void getData() { // public member function
        cout << "Enter roll, grade and percentage: ";
        cin >> roll >> grade >> per;
    }

    void showData() { // public member function
        cout << "Roll: " << roll << endl;
        cout << "Grade: " << grade << endl;
        cout << "Percentage: " << per << endl;
    }
};

int main() {
    Student s1;  // object of class

    s1.getData();  // access public member function
    s1.showData(); // access public member function

    return 0;
}

9. Program.

Questiob: Write an object-oriented Cpp program to calculate and print the sum of two integers given by the user.

#include <iostream>
using namespace std;

class Num {
private:
    int a, b, c;   

public:
    void get() {
        cout << "Enter 2 integers: ";
        cin >> a >> b;
    }

    void add() {
        c = a + b;
    }

    void show() {
        cout << "Numbers are " << a << " and " << b << endl;
        cout << "Their sum is " << c << endl;
    }
};

int main() {
    Num obj;      

    obj.get();     
    obj.add();     
    obj.show();    

    return 0;
}

10. What is Relation between OOP and Cpp

Aspect OOP (Object-Oriented Programming) C++
Definition Methodology or approach for writing/developing programs Programming language based on OOP principles
Purpose Teaches principles to make programs secure, reusable, and model real-world situations Provides syntax and tools to implement OOP concepts like classes, objects, encapsulation, inheritance, polymorphism
Data Security Emphasizes protecting data from unauthorized access Uses keywords like private to secure data
Origin Concept started in 1967 Implemented in C++ (supports OOP concepts)
Example OOP says: data should be private In C++: class Student { private: int roll; };
First OOP Language SIMULA 67 (used in labs for simulation) C++ provides practical implementation of OOP

 

Page 2 of 2