Lecture 3
Lecture 3
Lecture 3
(OOP)
Lecture 03
CONTENT
Examples: Example 4
Examples: Example 4 Output
Examples: Example 4 Code Explanation
Examples: Example 5
Examples: Example 5 Output
Examples: Example 5 Code Explanation
Class Methods: Inside and Outside the Class
Class Methods: Outside the Class (Syntax)
Class Methods: Outside the Class (Example)
Class Methods: Outside the Class with Parameters
CONTENT
Constructors: Syntax
Constructors: Example
Constructors: Passing Parameters to Constructors
Constructors: Passing Parameters to Constructors (Example)
Constructors: Defining Outside the Class
Constructors: Defining Outside the Class (Example)
Constructors: How A Constructor Works (Example)
3
4
Examples: Example 4
Example 4: Demonstrates the use of multiple classes and creating multiple objects from those classes.
CODE EXPLANATION
1. Two Classes:
Class Person: Has attributes name and age and a method displayPersonInfo() to display the details of a person.
Class Car: Has attributes brand, model, and year and a method displayCarInfo() to display the details of a car.
2. Objects:
We create two objects of the Person class: person1 and person2. Each has different values for name and age.
We create two objects of the Car class: car1 and car2. Each has different values for brand, model, and year.
3. Method Calls:
We use person1.displayPersonInfo() and person2.displayPersonInfo() to display the information of both Person objects.
Similarly, car1.displayCarInfo() and car2.displayCarInfo() are used to display the information of both Car objects.
When we want to store numbers with 7
decimals, we can either use the float or
double.
However, doubles have twice the byte size
of floats. Also, doubles are more accurate
Examples: Example 5
when dealing with large decimal values.
Example 5: A Bank Account class that implements a balance check mechanism, restricted withdrawal, and interest calculation.
Examples: Example 5
Example 5: A Bank Account class that implements a balance check mechanism, restricted withdrawal, and… [continued]
else if (amount > 0) { int main() {
balance -= amount; // Creating a unique object with an initial balance and interest rate
cout << "Withdrawal successful. Current balance: $" << balance << BankAccount account1("John Doe", 500.0, 2.5); // 2.5% interest rate
endl;
} else { // Display account info
cout << "Invalid withdrawal amount." << endl; account1.displayAccountInfo(); // Output: Account Holder: John Doe,
} Balance: $500
}
// Method to apply annual interest // Try to withdraw more money than available (special case scenario)
void applyInterest() { account1.withdraw(600); // Output: Insufficient funds
double interest = (balance * interestRate) / 100;
balance += interest; // Deposit some money
cout << "Interest applied at " << interestRate << "%. Current balance: account1.deposit(200); // Output: Deposit successful. Current balance:
$" << balance << endl; $700
}
// Successful withdrawal
// Method to display account details account1.withdraw(300); // Output: Withdrawal successful. Current
void displayAccountInfo() { balance: $400
cout << "Account Holder: " << accountHolder << ", Balance: $" << // Apply interest
balance << endl; account1.applyInterest(); // Output: Interest applied at 2.5%. Current
} balance: $410
}; return 0;
}
9
OUTPUT
In the example below, a method named myMethod is defined within the class.
NOTE: Methods are accessed in the same way as attributes—by creating an instance (object) of the class and utilizing the dot notation (.).
SYNTAX: The syntax of defining a member function outside the class is as follows:
return_type class_name::function_name(parameters)
{ function body
}
COMPONENTS
return_type: Indicates the type of value to be returned by the function_name: The name of the member function to be
function. defined.
class_name: Indicates the name of the class to which the parameters: A list of parameters (if any) that the function
function belongs. accepts.
::: Scope resolution operator. function body: The code that defines the function's behavior.
13
Constructors: Syntax
A constructor is a special member function that is automatically executed when an object of the class is created.
It has no return type and shares the same name as the class, followed by parentheses ().
A constructor functions like a normal function but cannot return any value. [i.e., programmer can't use a return statement to
return data from it.]
It is typically used to initialize the class's data members. [i.e. Its purpose is to initialize the object, not to provide a result. It
implicitly (automatically) returns the created object itself when it completes, but programmer do not specify or control this return.]
NOTE: The constructor has the same name as the class, it is always public, and it does not have any return value.
16
Constructors: Example
Example 4:
#include <iostream> int main() {
using namespace std; MyClass myObj; // Create an object of
MyClass (this will call the constructor)
class MyClass { // The class return 0;
public: // Access specifier }
MyClass() { // Constructor
cout << "Hello World!";
}
};
17
The key difference is that parameters are passed to the constructor when the object is created.
Parameters are enclosed in parentheses, along with the object name, during the declaration.
type object_name(parameters);
type: Represents the class name, indicating the type of object that will be instantiated.
Then, define the constructor outside the class using the following syntax:
Follow with the constructor name, which matches the class name.
SYNTAX:
ANY QUESTIONS?
23