C++ Unit 1
C++ Unit 1
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
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>
class Bank {
private:
string name;
char type[10];
void setvalue() {
cin.ignore();
getline(cin, name);
void showdata() {
void deposit() {
void showbal() {
void withdrawl() {
int a, avai_balance;
cin >> a;
avai_balance = tot - a;
};
int main() {
Bank b;
int choice;
while (1) {
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
<< "~~~WELCOME~~~~~~~~~~~~~~~~~~"
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
<< "~~~~~~~~~\n\n";
case 1:
b.setvalue();
break;
default:
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.