Object Oriented Programming: Chapter 2: Classes and Objects

Access Specifiers

C++ Programming | Object Oriented Programming

Inside the body of the class the data members and functions are declared using the keywords like private, public or protected. These are called access specifiers.

Access Specifiers

•  Inside the body of the class the data members and functions are declared using the keywords like private, public or protected. These are called access specifiers.

•  The access specifier specifies the manner in which the data can be accessed.

•  By default the access specifier is of private type. When the data and functions are declared as private then only members of same class can access them. This achieves the data hiding property.

•  The public access specifier allows the function declared outside the class to access the data and functions declared under it.

•  The protected access specifier allows the functions and data declared under it be accessible by the belonging class and the immediate derived class. Outside the belonging class and derived class these members are not accessible.

•  The accessibility of members of class is as shown in  2.3.1.


Fig. 2.3.1 Accessibility for members of class

 

Example:1

Create a class account. It has three data member account id, name and balance. Define function to assign value and display value. Define function that search account number given by the user. If account number exists, print detail of that account. Write a program using array of object. Declare at least 5 account and print details.

Solution:                               

#include<iostream>

using namespace std;

class Account

{

    private:

        int AccId;

        char Name[20];

        double balance;

    public:

        void get_data();

        void search(int);

};

void Account::get_data()

{

    cout<<"\n Enter the Account Number: ";

    cin>>AccId;

    cout<<"\n Enter the Name: ";

    cin>>Name;

    cout<<"\n Enter the Account balance: ";

    cin>>balance;

}

void Account::search(int i)

if(AccId ==i)

{

    cout<<"\n Account Details : ";

    cout<<"\n\n Account Number: "<<AccId;

    cout<<"\n Name :"<<Name;

    cout<<"\n Balance : "<<balance;

}

else

    return;

}

int main()

{

    Account obj[5];

    int i,n,num;

    cout<<"\n How many records are there? ";

    cin>>n;

    cout<<<"\n Enter the records....\n";

    for(i=0;i<n;i++)

    {

        cout<<"\n\t\t Record # "<<i+1;

        obj[i].get_data();

    }

    cout<<"\n Enter the Account Number for displaying the details: ";

    cin>>num;

    for(i=0;i<n;i++)

    {

         obj[i].search(num);

    }

    return 0;

}

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Access Specifiers


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation