Class and Objects
Class and Objects
public:
// Data members
string make;
string model;
int year;
// Member function
void displayInfo() {
cout << "Car Make: " << make << ", Model: " << model << ",
Year: " << year << endl;
}
};
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 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.