0% found this document useful (0 votes)
24 views17 pages

Ii B.com Lab Exercise

The document contains multiple C++ programming examples demonstrating key concepts such as classes, constructors, operator overloading, inheritance, and pointers. Each section includes a specific aim, code implementation, and a brief explanation of the functionality, such as managing bank accounts, calculating sums of digits, performing complex number addition, matrix multiplication, and checking for palindromes. The examples serve as practical applications of C++ programming principles.

Uploaded by

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

Ii B.com Lab Exercise

The document contains multiple C++ programming examples demonstrating key concepts such as classes, constructors, operator overloading, inheritance, and pointers. Each section includes a specific aim, code implementation, and a brief explanation of the functionality, such as managing bank accounts, calculating sums of digits, performing complex number addition, matrix multiplication, and checking for palindromes. The examples serve as practical applications of C++ programming principles.

Uploaded by

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

1.

CLASSES

Aim
To Write a Program using a class to represent a Bank Account with Data Members –
Name of depositor, Account Number, Type of Account and Balance and Member
Functions – Deposit Amount – Withdrawal Amount. Show name and balance. Check the
program with own data.

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

class bank
{
int acno;
char nm[100], acctype[100];
float bal;
public:
bank(int acc_no, char *name, char *acc_type, float balance) //Parameterized
Constructor
{
acno=acc_no;
strcpy(nm, name);
strcpy(acctype, acc_type);
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void bank::deposit() //depositing an amount
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void bank::withdraw() //withdrawing an amount
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void bank::display() //displaying the details
{
cout<<"\n ----------------------";
cout<<"\n Accout No. : "<<acno;
cout<<"\n Name : "<<nm;
cout<<"\n Account Type : "<<acctype;
cout<<"\n Balance : "<<bal;
}
int main()
{
int acc_no;
char name[100], acc_type[100];
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Accout No. ";
cin>>acc_no;
cout<<"\n Name : ";
cin>>name;
cout<<"\n Account Type : ";
cin>>acc_type;
cout<<"\n Balance : ";
cin>>balance;

bank b1(acc_no, name, acc_type, balance); //object is created


b1.deposit(); //
b1.withdraw(); // calling member functions
b1.display(); //
return 0;
}
OUTPUT :
2. Constructor & Destructor

AIM:
To Write a program to read an integer and find the sum of all the digits until it reduces to a
single digit using constructor, destructor and default constructor.

#include <iostream>

using namespace std;

class SumofDigits {

public:
long int n;
SumofDigits()
{
n=0;

}
inline void getInteger() {
cout << "Enter an Integer Number:";
cin >> n;
}

int sum()
{
int s = 0, r;

while (n>0||s>9) {
if(n == 0)
{
n = s;
s = 0;
}
r = n % 10;
s = s + r;
n = n / 10;
}
return s;
}
~SumofDigits()
{
cout<<"End of Program. Object Deleted";
}
};

int main() {

SumofDigits SD;
int result;
cout<<"Sum Of Digits"<<endl;
cout<<"*************"<<endl;
SD.getInteger();
result = SD.sum();
cout << "Sum of all digits of a number is " << result<<endl;
return 0;
}

3. Operator Overloading
Aim
To write a C++ program to manipulate Addition of Two Complex Numbers using operator
overloading.

#include <iostream>
#include <sstream>
#include <cmath>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(){
real = imag = 0;
}
Complex (int r, int i){
real = r;
imag = i;
}
string to_string(){
stringstream ss;
if(imag >= 0)
ss << "(" << real << " + " << imag << "i)";
else
ss << "(" << real << " - " << abs(imag) << "i)";
return ss.str();
}
Complex operator+(Complex c2){
Complex ret;
ret.real = real + c2.real;
ret.imag = imag + c2.imag;
return ret;
}
};
int main(){
Complex c1(8,-5), c2(2,3);
Complex res = c1 + c2;
cout << res.to_string();
}

Output:

B) Operator Overloading
Aim
To write a C++ program to calculate matrix multiplication using operator overloading

#include<iostream>

using namespace std;

class matrix
{

public:
int a[3][3];

matrix()//default constructor
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=0;
}
}
}
void set()// to set matrix elements
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"\n Enter "<<i<<","<<j<<" element=";
cin>>a[i][j];
}
}
}
void show()// to show matrix elements
{
cout<<"\n Matrix is=\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{

cout<<a[i][j]<<",";
}
cout<<"\n";
}
}

matrix operator*(matrix x)// overloading * for multiplication


{
matrix c;// this will hold our result
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c.a[i][j]=0;
for(int k=0;k<3;k++)
{
c.a[i][j]=c.a[i][j]+a[i][k]*x.a[k][j];
}
}
}
return(c);

}
};

int main()
{
matrix a,b,c;
a.set();
b.set();
c=a*b;

a.show();
b.show();
c.show();
}
Aim
4. Inheritance

Aim:

To write a C++ program to prepare payroll of an employee using inheritance

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class emp.
Step 3: Define and declare the function get() to get the employee details.
Step 4: Declare the derived class salary.
Step 5: Declare and define the function get1() to get the salary details.
Step 6: Define the function calculate() to find the net pay.
Step 7: Define the function display().
Step 8: Create the derived class object.
Step 9: Read the number of employees.
Step 10: Call the function get(),get1() and calculate() to each employees.
Step 11: Call the display().
Step 12: Stop the program.
PROGRAM:PAYROLL SYSTEM USING SINGLE INHERITANCE
1#include<iostream.h>
2#include<conio.h>
3
4class emp
5{
6 public:
7 int eno;
8 char name[20],des[20];
9 void get()
1 {
0 cout<<"Enter the employee number:";
1 cin>>eno;
1 cout<<"Enter the employee name:";
1 cin>>name;
2 cout<<"Enter the designation:";
1 cin>>des;
3 }
1};
4
1class salary:public emp
5{
1 float bp,hra,da,pf,np;
6 public:
1 void get1()
7 {
1 cout<<"Enter the basic pay:";
8 cin>>bp;
1 cout<<"Enter the Humen Resource Allowance:";
9 cin>>hra;
2 cout<<"Enter the Dearness Allowance :";
0 cin>>da;
2 cout<<"Enter the Profitablity Fund:";
1 cin>>pf;
2 }
2 void calculate()
2 {
3 np=bp+hra+da-pf;
2 }
4 void display()
2 {
5 cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<
2 "\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";
6 }
2};
7
2void main()
8{
2 int i,n;
9 char ch;
3 salary s[10];
0 clrscr();
3 cout<<"Enter the number of employee:";
1 cin>>n;
3 for(i=0;i<n;i++)
2 {
3 s[i].get();
3 s[i].get1();
3 s[i].calculate();
4 }
3 cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
5 for(i=0;i<n;i++)
3 {
6 s[i].display();
3 }
getch();
7}
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6

Output:
Enter the Number of employee:1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300

E.No E.name des BP HRA DA PF NP


150 ram Manager 5000 1000 500 300 6200

Output:
a) Pointer

Aim
To write a C++ Program to count Vowels in a string using Pointer

Algorithm

1. Initialize the string using a character array.


2. Create a character pointer and initialize it with the first element in array of character
(string).
3. Create a counter to count vowels.
4. Iterate the loop till character pointer find ‘\0’ null character, and as soon as null character
encounter, stop the loop.
5. Check whether any vowel is present or not while iterating the pointer, if vowel found
increment the count.
6. Print the count

#include <iostream>

using namespace std;

int vowelCount(char *sptr)


{
// Create a counter
int count = 0;

// Iterate the loop until null character encounter


while ((*sptr) != '\0') {

// Check whether character pointer finds any vowels


if (*sptr == 'a' || *sptr == 'e' || *sptr == 'i'
|| *sptr == 'o' || *sptr == 'u') {

// If vowel found increment the count


count++;
}

// Increment the pointer to next location


// of address
sptr++;
}
return count;
}

// Driver Code
int main()
{
// Initialize the string
char str[] = "geeksforgeeks";

// Display the count


cout << "Vowels in above string: " << vowelCount(str);

return 0;
}

b) palindrome

Algorithm:
1. Take two pointers say, ptr and rev.
2. Initialize ptr to the base address of the string and move it forward to point to the last
character of the string.
3. Now, initialize rev to the base address of the string and start moving rev in forward
direction and ptr in backward direction simultaneously until middle of the string is
reached.
4. If at any point the character pointed by ptr and rev does not match, then break from the
loop.
5. Check if ptr and rev crossed each other, i.e. rev > ptr. If so, then the string is palindrome
otherwise not.

Source code:

#include <stdio.h>

// Function to check if the string is palindrome


// using pointers
void isPalindrome(char* string)
{
char *ptr, *rev;

ptr = string;

while (*ptr != '\0') {


++ptr;
}
--ptr;
for (rev = string; ptr >= rev;) {
if (*ptr == *rev) {
--ptr;
rev++;
}
else
break;
}

if (rev > ptr)


printf("String is Palindrome");
else
printf("String is not a Palindrome");
}

// Driver code
int main()
{
char str[1000] = "madam";

isPalindrome(str);

return 0;
}

You might also like