C++ Programming Practical Programs for Students | Call by Reference, Constructor Overloading and Account Class
C++ Programming Concepts and Object-Oriented Programming
Introduction
C++ is one of the most powerful and popular programming languages used in computer science and software development. It is an extension of the C programming language and supports both procedural programming and object-oriented programming (OOP). C++ is widely used for developing system software, application software, games, embedded systems, and many other types of programs because of its speed, flexibility, and efficiency.
Object-Oriented Programming is an important feature of C++. OOP helps programmers organize programs into objects and classes, making programs easier to manage, reuse, and understand. Some important concepts of OOP include classes, objects, constructors, encapsulation, inheritance, and polymorphism.
In this article, we will discuss three important C++ programming concepts:
Exchange of two variables using Call by Reference
Implementation of an Account Class with Deposit, Withdraw, Balance, and Interest Functions
These concepts are very useful for understanding the fundamentals of C++ programming and object-oriented programming.
1. Exchange of Two Variables Using Call by Reference
Introduction to Call by Reference
In C++, functions can receive arguments in different ways. One important method is Call by Reference. In this method, instead of sending copies of variables, the actual memory addresses or references of variables are passed to the function. Therefore, any changes made inside the function directly affect the original variables.
Call by Reference is useful because:
It avoids unnecessary copying of data.
It improves memory efficiency.
It allows functions to modify original values.
It is commonly used in swapping operations.
The symbol & is used to represent references in C++.
Objective of the Program
The objective of this program is:
To understand the concept of Call by Reference.
To exchange or swap the values of two variables.
To demonstrate how references work in C++.
Algorithm for Swapping Two Variables
Start the program.
Declare two variables.
Create a function with reference parameters.
Use a temporary variable for swapping.
Exchange the values.
Display the swapped values.
End the program.
C++ Program for Swapping Using Call by Reference
C++
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int x, y;
cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;
cout << "Before swapping: ";
cout << "x = " << x << " y = " << y << endl;
swap(x, y);
cout << "After swapping: ";
cout << "x = " << x << " y = " << y << endl;
return 0;
}
Explanation of the Program
In this program:
swap() function receives variables by reference.
temp temporarily stores one value.
Since references are used, original values change directly.
For example:
If:
C++
x = 10
y = 20
After swapping:
C++
x = 20
y = 10
Advantages of Call by Reference
Faster execution
Saves memory
Allows direct modification
Useful for large programs
2. Constructor Overloading in C++
Introduction to Constructors
A constructor is a special member function in C++ that is automatically called when an object is created. The main purpose of a constructor is to initialize objects.
Characteristics of constructors:
Constructor name is same as class name.
Constructors have no return type.
Automatically executed during object creation.
There are different types of constructors:
Default Constructor
Parameterized Constructor
Copy Constructor
When multiple constructors are defined in the same class with different parameters, it is called Constructor Overloading.
Objective of Constructor Overloading
The objectives are:
To initialize objects in different ways.
To demonstrate polymorphism in constructors.
To improve flexibility in object creation.
Example of Constructor Overloading
Suppose we create a class called Student.
One constructor initializes default values.
Another constructor accepts student details.
This demonstrates constructor overloading.
C++ Program for Constructor Overloading
C++
#include <iostream>
using namespace std;
class Student
{
int roll;
string name;
// Default constructor
Student()
{
roll = 0;
name = "Unknown";
}
// Parameterized constructor
Student(int r, string n)
{
roll = r;
name = n;
}
void display()
{
cout << "Roll Number: " << roll << endl;
cout << "Name: " << name << endl;
}
};
int main()
{
Student s1;
Student s2(101, "Rahul");
cout << "Student 1 Details:" << endl;
s1.display();
cout << endl;
cout << "Student 2 Details:" << endl;
s2.display();
return 0;
}
Explanation of the Program
In this program:
Student() is a default constructor.
Student(int r, string n) is a parameterized constructor.
Both constructors have the same name but different parameters.
This is called constructor overloading.
Output Example
C++
Student 1 Details:
Roll Number: 0
Name: Unknown
Student 2 Details:
Roll Number: 101
Name: Rahul
Advantages of Constructor Overloading
Flexibility in object creation
Better code readability
Multiple initialization methods
Saves programming time
Real-Life Example
Constructor overloading can be compared to filling a form:
Sometimes complete information is available.
Sometimes only partial information is available.
Different constructors handle different situations.
3. Account Class Implementation in C++
Introduction to Classes and Objects
A class is a user-defined data type in C++ that contains data members and member functions.
An object is an instance of a class.
Classes help in:
Code reusability
Encapsulation
Organized programming
systems commonly use classes for account management.
Objective of the Account Class Program
The objectives are:
To create a account system.
To perform deposit and withdrawal operations.
To calculate interest.
To display account balance.
Features of the Account Class
Account balance
Deposit function
Withdraw function
Show balance function
Compute interest function
C++ Program for Account Class
C++
#include <iostream>
using namespace std;
class Account
{
float balance;
float rate;
public:
void initialize(float b, float r)
{
balance = b;
rate = r;
}
void deposit(float amount)
{
balance = balance + amount;
cout << "Amount Deposited Successfully" << endl;
void withdraw(float amount)
{
if(amount <= balance)
{
balance = balance - amount;
cout << "Amount Withdrawn Successfully" << endl;
}
else
{
cout << "Insufficient Balance" << endl;
}
}
void showBalance()
{
cout << "Current Balance: " << balance << endl;
}
void computeInterest()
{
float interest;
interest = (balance * rate) / 100;
cout << "Interest: " << interest << endl;
}
};
int main()
{
Account a1;
a1.initialize(10000, 5);
a1.showBalance();
a1.deposit(2000);
a1.showBalance();
a1.withdraw(3000);
a1.showBalance();
a1.computeInterest();
return 0;
}
Explanation of the Program
initialize()
This function initializes account balance and interest rate.
Adds money to the account balance.
withdraw()
Subtracts money if sufficient balance exists.
showBalance()
Displays the current account balance.
computeInterest()
Calculates interest using the formula:
�
Example Calculation
If:
Balance = 10000
Rate = 5%
Then:
�
So, the interest will be ₹500.
Advantages of Using Classes in B Systems
Data protection
Easy maintenance
Real-world representation
Better security
Importance of Object-Oriented Programming in C++
Object-Oriented Programming is highly important in modern software development. It allows developers to build large applications efficiently.
Benefits of OOP:
Reusability
Modularity
Security
Easy debugging
Better maintenance
Applications of OOP include:
Hospital management systems
School management systems
E-commerce applications
Mobile applications
Conclusion
C++ is a powerful programming language that supports object-oriented programming concepts. In this article, we studied three important programs:
Swapping two variables using Call by Reference
Constructor Overloading
Account Class Implementation
The first program explained how references allow direct modification of variables. The second program demonstrated constructor overloading, which provides multiple ways to initialize objects. The third program implemented a banking account system using classes and member functions.
These programs help students understand the practical implementation of OOP concepts in C++. They are essential for students and beginner programmers because they provide a strong foundation for advanced programming and software development.
By learning these concepts carefully, students can improve their programming skills and develop efficient real-world applications in C++.
Comments
Post a Comment
Thanks for sharing your thoughts! Stay tuned for more updates