Skip to main content

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
Constructor Overloading in C++
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.


Values are exchanged using assignment statements.
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;

public:

    // 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:
Data security
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
The account class includes:
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.
deposit()
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
Organized code
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

Popular posts from this blog

How to Generate Images with Gemini AI and Convert Them into Videos

Introduction Artificial Intelligence Artificial Intelligence has completely changed the way we create and share digital content. One of the most exciting innovations is Gemini AI, Google’s advanced multimodal AI model that can work with text, images, and more. With Gemini AI, you can generate realistic and creative images just by giving a text prompt. Once you have the images, you can also convert them into professional-looking videos for YouTube, Instagram, Facebook, or Blogger. In this article, you will learn step by step how to generate AI images using Gemini AI and then how to turn those images into videos. This guide is written for beginners, so even if you are new to AI tools, you can follow along easily. --- What is Gemini AI? Gemini AI is Google’s latest artificial intelligence model, developed as an upgrade to Bard. Unlike traditional AI tools that focus only on text, Gemini is multimodal, meaning it can handle: Text Images Audio Code And more For content creators, the most po...

UGC Act Strengthening India’s Academic Integrity: Enforcing DigiLocker/NAD Verification and Cracking Down on Fake Universities

UGC Act Strengthening India’s Academic Integrity : Enforcing DigiLocker/NAD Verification and Cracking Down on Fake Universities Introduction In India, higher education and employment are deeply connected: degrees determine eligibility for jobs, further study, and professional credibility. Yet, a persistent problem continues to undermine the hopes and hard work of genuine graduates — fake or unrecognized universities issuing invalid degrees, leading to career setbacks, lost opportunities, and deep frustration among legitimate jobseekers.  The Times of India This Article explores:  What fake universities are How the University Grants Commission (UGC) Act 1956 defines degree-granting authority ✔ The role of digital systems like DigiLocker and National Academic Depository (NAD) in verification ✔ Why better policies are needed now ✔ A proposed roadmap to ensure fair employment for valid degree holders 1. What Are Fake or Unrecognized...

Future Skills That Will Create New Industries

Future Skills That Will Create New Industries (Human-led innovation in the age of advanced technology) built by machines alone. They will be imagined, designed, operated, and expanded by human curiosity, courage, and creativity.  Technology will act as a tool, but people will remain the core creators. As humanity prepares for space travel, aerial mobility, bio-design, climate engineering, and immersive realities, entirely new sectors will emerge—sectors that do not yet fully exist today. Below is a deep exploration of future skills and the new industries they will create, along with the kinds of jobs and opportunities that will arise for people. .1. Space Habitat Design New Industry: Human Living Systems in Space As space missions evolve from short visits to long-term habitation, humans will need environments where they can live, work, and thrive beyond Earth. This creates an industry focused on designing livable ecosyst...