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

Class and Objects

The document provides an overview of object-oriented programming concepts in C++, including classes, data members, member functions, and access specifiers. It illustrates these concepts through examples such as the Car, Person, Rectangle, and BankAccount classes, highlighting the use of the dot operator and the importance of encapsulation. Additionally, it explains how data can be passed to functions and the scope and visibility of variables.

Uploaded by

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

Class and Objects

The document provides an overview of object-oriented programming concepts in C++, including classes, data members, member functions, and access specifiers. It illustrates these concepts through examples such as the Car, Person, Rectangle, and BankAccount classes, highlighting the use of the dot operator and the importance of encapsulation. Additionally, it explains how data can be passed to functions and the scope and visibility of variables.

Uploaded by

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

class Car {

public:
// Data members
string make;
string model;
int year;

// Member function
void displayInfo() {
cout << "Car Make: " << make << ", Model: " << model << ",
Year: " << year << endl;
}
};

Car myCar; // myCar is an object of the Car class


myCar.make = "Toyota";
myCar.model = "Corolla";
myCar.year = 2020;
myCar.displayInfo(); // Output: Car Make: Toyota, Model: Corolla,
Year: 2020
Dot Operator
• The dot operator (.) is used to access the data members and
member functions of an object.
• It allows you to interact with the attributes and methods
defined in the class

myCar.make = "Honda"; // Accessing data member


myCar.displayInfo(); // Calling member function

Data Members
• Data members are variables that hold the state or attributes
of a class. They can be of any data type, including primitive
types, arrays, or other objects.
• Data members can have different access
specifiers: public, private, or protected.
EXAMPLE
class Person {
private:
string name; // Private data member
int age; // Private data member

public:
// Member functions to access private data members
void setName(string n) {
name = n;
}

void setAge(int a) {
age = a;
}

void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
Member Functions
• Member functions are functions defined within a class that
operate on the data members of the class. They can
manipulate the object's state and perform actions.
• Member functions can also have different access specifiers.

class Rectangle {
private:
int width, height;

public:
void setDimensions(int w, int h) {
width = w;
height = h;
}

int area() {
return width * height;
}
};
Passing Data to Functions
• Data can be passed to functions in several ways: by value, by
reference, or by pointer.
• By Value: A copy of the data is passed to the function. Changes
made to the parameter inside the function do not affect the
original data.
• By Reference: A reference to the original data is passed.
Changes made to the parameter inside the function affect the
original data.
• By Pointer: A pointer to the data is passed. This allows for
dynamic memory management and manipulation of the
original data.

EXAMPLE:
void updateAge(Person &p, int newAge) {
p.setAge(newAge); // Modifies the original object
}
Scope and Visibility of Variables in Functions
• Scope refers to the region of the program where a variable is
accessible. Variables can have different scopes:
• Local Scope: Variables declared inside a function are
local to that function and cannot be accessed outside of
it.
• Global Scope: Variables declared outside of all functions
are global and can be accessed from any function.
• Class Scope: Data members of a class are accessible to all
member functions of that class.
Example of Scope:
void myFunction() {
int localVar = 10; // Local variable
cout << localVar; // Accessible here
}

int main() {
myFunction();
// cout << localVar; // Error: localVar is not accessible here
}
Visibility
• Visibility refers to the accessibility of class members (data
members and member functions) based on their access
specifiers:
• Public: Members are accessible from outside the class.
• Private: Members are accessible only within the class.
• Protected: Members are accessible within the class and
by derived classes.
Example of Visibility:
class Example {
public:
int publicVar; // Accessible from anywhere
private:
int privateVar; // Accessible only within Example
protected:
int protectedVar; // Accessible within Example and derived
classes
};
class Derived : public Example {
public:
void accessMembers() {
publicVar = 1; // Accessible
// privateVar = 2; // Error: not accessible
protectedVar = 3; // Accessible
} };
EXAMPLE :
#include <iostream>
using namespace std;

class BankAccount {
private:
string accountHolder;
double balance;

public:
BankAccount(string name, double initialBalance) {
accountHolder = name;
balance = initialBalance;
}

void deposit(double amount) {


if (amount > 0) {
balance += amount;
cout << "Deposited: " << amount << endl;
} else {
cout << "Invalid deposit amount." << endl;
}
}
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrew: " << amount << endl;
} else {
cout << "Invalid withdrawal amount." << endl;
}
}

void displayBalance() {
cout << "Account Holder: " << accountHolder << ", Balance: "
<< balance << endl;
}
};

int main() {
BankAccount account("Alice", 1000.0);
account.displayBalance();
account.deposit(500);
account.withdraw(200);
account.displayBalance();
account.withdraw(1500);
return 0;
}
OUTPUT:
Account Holder: Alice, Balance: 1000
Deposited: 500
Withdrew: 200
Account Holder: Alice, Balance: 1300
Invalid withdrawal amount.

Process returned 0 (0x0) execution time : 0.060 s


Press any key to continue

Explanation of Concepts Used:


• Classes: The BankAccount class is defined to represent a bank
account.
• Abstraction & Encapsulation: The class encapsulates the data
members (accountHolder and balance) and provides public
member functions (deposit, withdraw, and displayBalance) to
interact with the data, hiding the internal implementation
details.
• Dot Operator: The dot operator is used to access the member
functions and data members of the BankAccount object
(e.g., account.deposit(500)).
• Data Members: accountHolder and balance are data members
of the BankAccount class.
• Member Functions: deposit, withdraw, and displayBalance are
member functions that operate on the data members.
• Passing Data to Functions: The deposit and withdraw functions
take parameters to specify the amount to deposit or withdraw.
• Scope and Visibility of Variables: The
variables accountHolder and balance are private, meaning they
cannot be accessed directly from outside the class. The
member functions control access to these variables.

You might also like