0% found this document useful (0 votes)
14 views11 pages

C++ Unit 1

Programming about C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

C++ Unit 1

Programming about C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Encapsulation

Encapsulation in C++ is defined as the wrapping up of data and


information in a single unit. In Object Oriented Programming,
Encapsulation is defined as binding together the data and the
functions that manipulate th`em.

Two Important property of Encapsulation


1. Data Protection: Encapsulation protects the internal state of an object by
keeping its data members private. Access to and modification of these data
members is restricted to the class’s public methods, ensuring controlled
and secure data manipulation.
2. Information Hiding: Encapsulation hides the internal implementation
details of a class from external code. Only the public interface of the class
is accessible, providing abstraction and simplifying the usage of the class
while allowing the internal implementation to be modified without
impacting external code.

1.
o #include <iostream>
o using namespace std;
o
o class Person {
o private:
o string name;
o int age;
o
o public:
o Person(string name, int age) {
o this->name = name;
o this->age = age;
o }
o
o // Other member functions...
o };
o
o int main() {
o Person person("Alice", 30);
o // ...
o return 0;
o }
o

Data abstraction is a fundamental concept in object-oriented programming


(OOP), including C++. It involves presenting only the essential information
while concealing the intricate implementation details. Let’s delve into what data
abstraction is and how it can be implemented in C++:
1. Definition of Data Abstraction:
o Data abstraction means providing a simplified view of an object
or system by revealing only the necessary information and hiding
the underlying complexity.
o Imagine a car driver who knows how to accelerate or apply brakes
but doesn’t need to understand the inner workings of 00the car’s
mechanisms. This encapsulation of essential functionality while
hiding implementation details exemplifies data abstraction.
2. Types of Abstraction:
o Data Abstraction: Reveals only the required information about the
data and conceals unnecessary details.
o Control Abstraction: Shows essential information about the
implementation while hiding extraneous details.
3. Implementing Data Abstraction in C++:
o Classes: We use classes to implement data abstraction. A class
groups data members (attributes) and member functions (methods)
using access specifiers.
o Access Specifiers:
 Public: Members declared as public can be accessed from
anywhere in the program.
 Private: Members declared as private are accessible only
within the class and not from outside.
o By marking internal implementation details as private and
exposing essential information as public, we achieve data
abstraction.

// C++ Program to Demonstrate the


// working of Abstraction
#include <iostream>
using namespace std;

class implementAbstraction {
private:
int a, b;

public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}

void display()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
#include <iostream>

#include <string>

using namespace std;

class Bank {

private:

string name;

long long accnumber;

char type[10];

long long amount = 0;

long long tot = 0;


public:

void setvalue() {

cout << "Enter name: ";

cin.ignore();

getline(cin, name);

cout << "Enter Account number: ";

cin >> accnumber;

cout << "Enter Account type: ";

cin >> type;

cout << "Enter Balance: ";

cin >> tot;

void showdata() {

cout << "Name: " << name << endl;

cout << "Account No: " << accnumber << endl;

cout << "Account type: " << type << endl;

cout << "Balance: " << tot << endl;

void deposit() {

cout << "\nEnter amount to be Deposited: ";

cin >> amount;

void showbal() {

tot = tot + amount;


cout << "\nTotal balance is: " << tot;

void withdrawl() {

int a, avai_balance;

cout << "Enter amount to withdraw: ";

cin >> a;

avai_balance = tot - a;

cout << "Available Balance is: " << avai_balance;

};

int main() {

Bank b;

int choice;

while (1) {

cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~"

<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

<< "~~~WELCOME~~~~~~~~~~~~~~~~~~"

<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

<< "~~~~~~~~~\n\n";

cout << "Enter Your Choice:\n";

cout << "\t1. Enter name, Account number, Account type\n";

// ... (other menu options)

cin >> choice;


switch (choice) {

case 1:

b.setvalue();

break;

// ... (other cases)

default:

cout << "Invalid choice. Try again.\n";

return 0;

}
Data abstraction is a fundamental concept in object-oriented programming (OOP),
including C++. It involves presenting only the essential information while concealing the
intricate implementation details. Let’s delve into what data abstraction is and how it can be
implemented in C++:
1. Definition of Data Abstraction:
o Data abstraction means providing a simplified view of an object or system by
revealing only the necessary information and hiding the underlying
complexity.
o Imagine a car driver who knows how to accelerate or apply brakes but doesn’t
need to understand the inner workings of the car’s mechanisms. This
encapsulation of essential functionality while hiding implementation details
exemplifies data abstraction.
2. Types of Abstraction:
o Data Abstraction: Reveals only the required information about the data and
conceals unnecessary details.
o Control Abstraction: Shows essential information about the implementation
while hiding extraneous details.
3. Implementing Data Abstraction in C++:
o Classes: We use classes to implement data abstraction. A class groups data
members (attributes) and member functions (methods) using access specifiers.
o Access Specifiers:
 Public: Members declared as public can be accessed from anywhere in
the program.
 Private: Members declared as private are accessible only within the
class and not from outside.
o By marking internal implementation details as private and exposing essential
information as public, we achieve data abstraction.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy