0% found this document useful (0 votes)
23 views146 pages

C and C++ Batch 2

This document outlines 11 experiments to be completed for a programming lab in C and C++. It provides the name, code, faculty, and dates for each experiment as well as space to record submission dates and marks. The experiments cover basic concepts like data types, control statements, functions and arrays as well as more advanced topics like classes, inheritance and operator overloading.

Uploaded by

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

C and C++ Batch 2

This document outlines 11 experiments to be completed for a programming lab in C and C++. It provides the name, code, faculty, and dates for each experiment as well as space to record submission dates and marks. The experiments cover basic concepts like data types, control statements, functions and arrays as well as more advanced topics like classes, inheritance and operator overloading.

Uploaded by

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

LAB NAME: PROGRAMMING IN C AND C++

LAB LAB CODE: SCSA2203

FACULTY NAME: DR.P.SIVAGAMI

SI. DATE OF NAME OF THE EXPERIMENT DATE OF MARKS STAFF


NO EXPERIMENT SUBMISSION SIGNATURE
1. 01 /02/2024 PROGRAMS USING BASIC DATA
TYPE AND I/O STATEMENTS
2. 08 /02/2024 PROGRAM USING CONTROL
STATEMENTS
3A. 08/02/2024 PROGRAMS USING FUNCTIONS

3B. 22/02/2024 PROGRAMS USING ARRAYS

4. 22 /02/2024 PROGRAMS USING STRUCTURE &


UNION
5. 07 /03/2024 PROGRAM ON DYNAMIC
MEMORY ALLOCATION
6. 07 /03/2024 PROGRAM ON FILE

7. 14/03/2024 PROGRAM USING CLASS AND


OBJECTS
8. 14/03/2024 PROGRAMS USING CONSTRUCTOR
AND DESTRUCTOR
9. 21/03/2024 PROGRAM USING INHERITANCE

10. 21/03/2024 PROGRAMS USING FUNCTION &


OPERATOR OVERLOADING
11. 04/04/2024 PROGRAMS USING VIRTUAL
FUNCTIONS
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:

OUTPUT:

USING FOR LOOP:

USING WHILE LOOP:


PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

#include
<stdio.h> void
main()
{
int n, reversedNumber = 0, remainder;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10+
remainder; n /= 10;
}
printf("\nReversed Number = %d", reversedNumber);
getch();
}

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
1.Write a c program to display output using ID

array ALGORITHM:
Step 1:Start the program
Step 2: Declare the variables
Step 3:User to enter the number of elements in the array
Step 4:User to enter the array elements.
Step 5:Read the elements of the array from the
user. Step 6:Print the array elements.
Step 7:End the program

FLOWCHART:

START

Readn

Call array elements

Displayvalues

STOP
PROGRAM:

OUTPUT:

2.Write a c program to display output using 2D

array ALGORITHM:
Step 1:Start the program

Step 2: Declare the variables


Step 3:User to enter the number of rows and columns of the matrix
Step 4:User to enter the elements of the matrix.
Step 5:Read the elements of the matrix from the user.
Step 6:Print the resultant matrix.
Step 7:End the program
FLOWCHART:

START

ReadI,j,n,m

Call resultant matrix

Displaymatrix

STOP

PROGRAM:
OUTPUT:

3.write a c program to compute the matrix addition using

array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of rows and columns of matrix A
Step 4:User to enter the number of rows and columns of matrix B
Step 5:If compatible, proceed with matrix addition, else print a message
indicating it's not possible.
Step 6:Read the elements of matrix A from the user.
Step 7:Read the elements of matrix B from the user.
Step 8: Addition corresponding elements of matrices A and B and store the
result in matrix C.
Step 9:End the program
FLOWCHART:

Start

ReadI,j,m,n

false
If((m==p)&
Matrixaddition
&(n==q))
is not possible

true
Fori=0tom

Forj=0ton

Reada[i][j],b[i][j]

C[i][j]=a[i][j]+b[i][j]

Displayc[i][j]

Stop
PROGRAM:
OUTPUT:

4.write a c program to compute the matrix multiplication using

array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of rows and columns of matrix A
Step 4:User to enter the number of rows and columns of matrix B
Step 5:If compatible, proceed with matrix multiplication, else print a message
indicating it's not possible.
Step 6:Read the elements of matrix A from the user.
Step 7:Read the elements of matrix B from the user.
Step 8:Multiplication corresponding elements of matrices A and B and store
the result in matrix C.
Step 9:End the program
FLOWCHART:

Start

ReadI,j,m,n

false
If((m==p)&
Matrixmultiplicationi
&(n==q))
s not possible

true
Fori=0tom

Forj=0ton

Reada[i][j]
a[i][j],b[i][j]

C[i][j]=c[i][j]+a[i][k]*b[k][j]

DisplayC[i][j]

Stop
PROGRAM:
OUTPUT:

5. write a c program to compute the matrix subtraction using

array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of rows and columns of matrix A
Step 4:User to enter the number of rows and columns of matrix B
Step 5:If compatible, proceed with matrix subtraction, else print a message
indicating it's not possible.
Step 6:Read the elements of matrix A from the user.
Step 7:Read the elements of matrix B from the user.
Step 8:Subtract corresponding elements of matrices A and B and store the
result in matrix C.
Step 9:End the program
FLOWCHART:

Start

ReadI,j,m,n

false
If((m==p)&
Matrixsubtractionis
&(n==q))
not possible

true
Fori=0tom

Forj=0ton

Reada[i][j]
a[i][j],b[i][j]

C[i][j]=a[i][j]-b[i][j]

Displayc[i][j]

Stop
PROGRAM:
OUTPUT:

6. write a c program to find the sum of n number using

array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the size of array
Step 4:Read the size of array
Step 5:User to enter the elements in the array

Step 6:Read the elements in the array from the


user. Step 7:Print sum of numbers
Step 8:End the program

FLOWCHART:

START

Readn
Sum=sum+arr[i]

Displaysum

STOP

PROGRAM:

OUTPUT:
7.write a c program to find the max element from n

numbers ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements

Step 6:Read the elements from the


user. Step 7:Print the largest number
Step 8:End the program
FLOWCHART:

START

Readn

Fori=0ton

If(a[i]>larg

Larg=a[0]

Displaylargest
number

STOP
PROGRAM:

OUTPUT:

8.write a c program to compute the ascending order program using

array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements
Step 6:Read the elements from the user.
Step 7:Print the sorted array in ascending order
Step 8:End the program
FLOWCHART:

START

Readn

Fori=0ton
If(i==n)
j=i+1ton

If(arr[j]<arr[i])

temp=arr[i]
arr[i]=arr[j]
arr[j]=temp

Displayascending
order

STOP

PROGRAM:
OUTPUT:

9.Write a c program to compute the linear search

program ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements
Step 6:Read the elements from the user.
Step 7:User to enter the element to search
Step 8:Read the element to search from the user.
Step 8:Perform linear search
Step 9:End the program
FLOWCHART:

START

Readn

Fori=0ton
If(i==n)

If(a[i]==search) If(i==n)

Larg=a[0] Elementisnotinthearray

Displaylargest
number

STOP
PROGRAM:

OUTPUT:

10. Write a c program to compute the descending order program using

array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements

Step 6:Read the elements from the user.


Step 7:Print the sorted array in descending order
Step 8:End the program
FLOWCHART:

START

Readn

Fori=0ton If(i==n)

j=i+1ton

If(arr[j]>arr[i])

temp=arr[i]
arr[i]=arr[j]
arr[j]=temp

Display
descendingorder

STOP
PROGRAM:
#include<stdio.h>
#include<conio.h
> void main()
{

int arr[50];
int
i,n,j,temp;
clrscr();
printf("\n\t\t DESCENDING ORDER OF A PROGRAM USING ARRAY:");

printf("\n\t\t -- ");
printf("\nenter the no.of elements in the array");
scanf("%d",&n);
printf("\nEnter the elements");
for(i=0;i<n;i++)
{

scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[j]>arr[i])
{
temp=arr[i]
;
arr[i]=arr[j];
arr[j]=temp
;
}
}
}
printf("\nThe descending order is:");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
getch();
}
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:

OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{

FILE *fptr1, *fptr2;


char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c=
fgetc(fptr1);
while (c != EOF)
{

fputc(c, fptr2);
c=
fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}

INPUT:

OUTPUT:
EXP:5 PROGRAM USING FILE
Storing Employee information
Step 1: Start the program.
Step 2: Declare the file and integers.
Step 3: Open the file in witting mode.
Step 4: Prompt the user to enter an integer id.
Step 5: Prompt the user to enter a string name.
Step 6: Prompt the user to enter a float salary.
Step 7: Write id, name, and salary to the file using fprintf.
Step 8: Close the file.
Step 9: Open the file in read mode.

Step 10: Read the integer,string,float from the file until end of file.
Step 11: Display the integer,string,float and Close the file.
Step 12: Stop the program.

PROGRAM:
#include
<stdio.h> int
main()
{

FILE *fptr;
int id;
char name[30];
float salary;
fptr = fopen("E:student1.txt","w”);
scanf("%d",&id);
scanf("%s", name);

scanf("%f",
&salary);
fclose(fptr);
fptr = fopen("E:student1.txt","r");
printf("Id=%d\n",id);
printf("Name=%s\n",name);
printf("salary=%.2f\n",
salary); fclose(fptr);
}

OUTPUT:
1. simple C++ program to add subtract multiply and divide two

numbers ALGORITHM:
Step 1:Start the program
Step 2:Declare variables in integers.
Step 3:Read the value of num1,num2 entered by the user.
Step 4:Calculate the sum of num1 and num2.
Step 5:Display the result of the sum.
Step 6:Calculate the difference between num1 and num2.
Step 7:Display the result of the difference.
Step 8:Calculate the product of num1 and num2.
Step 9:Display the result of the product.
Step 10:Calculate the division of num1 and num2.
Step 11:Display the result of the division.
Step 12:Stop the program

FLOWCHART:
START

Declareintegers

Readnum1,num
2

num1+num2

Displayadd

num1-num2

DisplaySub
num1*num2

DisplayMulti

num1/num2

DisplayDiv

STOP

PROGRAM:
#include<iostream>
using namespace std;
int main()
{

// declare variables
int num1, num2; // take input from end-user
cin >> num1>>num2;

// addition of two numbers


cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;

// subtraction of two numbers


cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;

// multiplication of two numbers


cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;

// division of two numbers


cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
return 0;
}
OUTPUT:

2. C++ Program for addition,subtraction,multiplication,division using class and


object declare the function inside the class

ALGORITHM:
Step 1:Start the program
Step 2:Declare variables in integers.
Step 3:Read the value of num1,num2 entered by the user.
Step 4: Call add method by passing num1 and num2 as arguments and store the result in a.
Step 5: Display the result of a with label "Addition".
Step 6: Call subtract method by passing num1 and num2 as arguments and store the result
in b.
Step 7: Display the result of b with label "Subtraction".
Step 8: Call muliply method by passing num1 and num2 as arguments and store the result in
c.
Step 9: Display the result of c with label "Muliplication".
Step 10: Call divide method by passing num1 and num2 as arguments and store the result in
d.
Step 11: Display the result of d with label "Division".
Step 12:Stop the program
FLOWCHART:
START

Declareintegers

Readnum1,num
2

a=ar.add(num1,num2)

DisplayAddition=

b=ar.subtracrt(num1,num2)

DisplaySubtraction=

c=ar.multiply(num1,num2)

DisplayMultiplication=

d=ar.divide(num1,num

DisplayDivision=

STOP
PROGRAM:
#include<iostream.h>
using namespace std;
class Arithmetic
{ public:
// function to add two numbers
int add(int n1, int n2)
{
return n1+n2;
}
// function to subtract two numbers
int subtract(int n1, int n2)
{
return n1-n2;
}
// function to multiply two numbers
int multiply(int n1, int n2)
{
return n1*n2;
}
// function to divide two numbers
int divide(int n1, int n2)
{
return n1/n2;
}
};
int main()
{
// declare variables
int num1, num2;
int a,b,c,d;
// declare object of Arithmetic class
Arithmetic ar;
cin >> num1 >> num2; a=ar.add(num1, num2);

// addition of two number


cout << "Addition="<<a<< endl;

b=ar.subtract(num1, num2);

// subtraction of two number


cout << "Subtraction="<<b<< endl;

c=ar.multiply(num1, num2);

// multiplication of two number

cout << "Multiplication="<<c<< endl;

d=ar.divide(num1, num2);

// division of two number

cout << "Division="<< d<< endl;

return 0;

}
OUTOUT:

3. C++ Program for addition,subtraction,multiplication,division using class and


object declare the function outside the class
ALGORITHM:
Step 1:Start the program
Step 2:Declare variables in integers.
Step 3:Read the value of num1,num2 entered by the user.
Step 4: Call add method by passing num1 and num2 as arguments and store the result in a.
Step 5: Display the result of a with label "Addition".
Step 6: Call subtract method by passing num1 and num2 as arguments and store the result
in b.
Step 7: Display the result of b with label "Subtraction".
Step 8: Call muliply method by passing num1 and num2 as arguments and store the result in
c.
Step 9: Display the result of c with label "Muliplication".
Step 10: Call divide method by passing num1 and num2 as arguments and store the result in
d.
Step 11: Display the result of d with label "Division".
Step 12:Stop the program
FLOWCHART:

START

Declareintegers

Readnum1,num
2
a=ar.add(num1,num2)

DisplayAddition=

b=ar.subtracrt(num1,num2)

DisplaySubtraction=

c=ar.multiply(num1,num2)

DisplayMultiplication=

d=ar.divide(num1,num

DisplayDivision=

STOP

PROGRAM:
#include<iostream.h>
using namespace std;
class Arithmetic
{ public:
// function to add two numbers
int add(int n1, int n2);
// function to subtract two numbers
int subtract(int n1, int n2);
// function to multiply two numbers
int multiply(int n1, int n2);
// function to divide two numbers
int divide(int n1, int n2);
};
int Arithmetic::add(int n1, int n2)
{
return n1+n2;
}
int Arithmetic::subtract(int n1, int n2)
{
return n1-n2;
}
int Arithmetic::multiply(int n1, int n2)
{
return n1*n2;
}
int Arithmetic::divide(int n1, int n2)
{
return n1/n2;
}
int main()
{
// declare
variables int num1,
num2; int a,b,c,d;
// declare object of Arithmetic class
Arithmetic ar;
cin >> num1 >> num2; a=ar.add(num1, num2);
// addition of two number
cout << "Addition="<<a<< endl;
b=ar.subtract(num1, num2);
// subtraction of two number

cout << "Subtraction="<<b<< endl;


c=ar.multiply(num1, num2);
// multiplication of two number
cout << "Multiplication="<<c<<
endl; d=ar.divide(num1, num2);
// division of two number

cout << "Division="<< d<< endl;


return 0;
}
OUTPUT:

4. Read and print details of a student using class program in

C++ ALGORITHM:
Step 1:Start the program
Step 2: Define the student class with private member variables: name, rollNo, total, and
perc.
Step 3:Define two public member functions: getDetails and putDetails.
Step 4:In the getDetails function, get the student's name, roll number, and total marks from
the user. Calculate the percentage and store it in the perc member variable.
Step 5:In the putDetails function, print the student's details to the console.
Step 6:In the main function, create an object of the student class.
Step 7:Call the getDetails function to get the student's details.
Step 8:Call the putDetails function to print the student's details.
Step 9:Return 0 to indicate successful execution of the program.
Step 10:End the program
FLOWCHART:

PROGRAM:
#include <iostream.h>
#include <iomanip.h>
using namespace;
class student
{ private:
char
name[30]; int
rollNo;
int total;
float
perc;
public:
// member function to get student's details
void getDetails(void);
// member function to print student's
details void putDetails(void);
};
// member function definition, outside of the class
void student::getDetails(void)
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number:
"; cin >> rollNo;
cout << "Enter total marks out of 500: ";
cin >> total;
perc = float(total) / 500 * 100;
}
// member function definition, outside of the class
void student::putDetails(void)
{

cout << "Student details: \n";


cout << "Name: " << name << ", Roll Number: " << rollNo << ", Total: " << total << ",
Percentage: " << perc;
}
int main()
{ student
std;
// object creation
std.getDetails();
std.putDetails();
return 0;
}
OUTPUT:
1. PROGRAM USING CONSTRUCTOR AND DESTRUCTOR

ALGORITHM:
Step 1:start the program
Step 2:Define a class Demo with private data members num1 and num2.
Step3:Inside the class Demo, define a public constructor to initialize num1 and num2.
Step 4:Inside the constructor, print "Inside Constructor" to indicate its execution and
initialize num1 with n1 and num2 with n2.
Step 5:Define a public member function display() to display the values of num1 and num2.
Step 6:Inside the display() function, print the values of num1 and num2.
Step 7:Define a destructor ~Demo() to perform cleanup tasks when an object of the class
Demo goes out of scope.
Step 8:Inside the destructor, print "Inside Destructor" to indicate its execution.
a. Create an object obj1 of class Demo with initial values 10 and 20.
b. Call the display() function to display the values of num1 and num2 for the object
obj1. Step 9:end the program
FLOWCHART:

PROGRAM:
}

OUTPUT:

2. PROGRAM USING CONSTRUCTOR


ALGORITHM:
Step 1:start the program
Step 2:Define a class bank with private data members acno, nm, acctype, and bal.
Step 3:Define public member functions: bank(int acc_no, char *name, char *acc_type, float
balance) as a parameterized constructor, deposit() to deposit an amount, withdraw() to
withdraw an amount, and display() to display account details.
Step 4:Inside the parameterized constructor, initialize the private members with the values
passed as arguments.
Step 5:Implement the deposit() function to accept an amount and add it to the balance.
Step 6:Implement the withdraw() function to accept an amount and deduct it from the
balance, checking if sufficient balance is available.
Step 7:Implement the display() function to display the account details.
In the main() function:
a. Declare variables acc_no, name, acc_type, and balance.
b. Accept input for acc_no, name, acc_type, and balance.
c. Create an object b1 of class bank using the constructor with the provided inputs.
d. Call the deposit(), withdraw(), and display() functions for the object
b1. Step 8:End the program
PROGRAM:
OUTPUT:
PROGRAM USING INHERITANCE
1. //C++ PROGRAM TO EXPLAIN SINGLE INHERITANCE
ALGORITHM:
Step 1:start the program
Step 2:Define the Vehicle class with a constructor that prints the message "This is a
Vehicle" to the console.
Step 3:Define the Car class that is derived from the Vehicle class.
Step 5:In the Car class, define a constructor that prints the message "This is a Car" to the
console.
Step 6:In the main function, create an object of the Car class.
Step 7:When the Car object is created, the constructors of both the Car and Vehicle classes
will be called.
Step 8:end the program
FLOWCHART:

PROGRAM:
#include <iostream>
using namespace std;
// base class
class
Vehicle
{
public:
Vehicle(
)
{
cout << "This is a Vehicle" << endl;
}
};
// sub class derived from the base classes
// although this class does NOTHING
class Car: public Vehicle
{

public:
Car()
{
cout << "This is a Car" << endl;
}
};
// main function
int main()
{

// creating object of sub class will


// invoke the constructor of base classes
Car obj;
return 0;
}
OUTPUT:

2. //C++ PROGRAM TO EXPLAIN


MULTIPLE INHERITANCE
ALGORITHM:
Step 1:start the program
Step 2:Define the Vehicle class with a constructor that prints the message "This is a
Vehicle" to the console.
Step 3:Define the FourWheeler class with a constructor that prints the message "This is a 4
wheeler Vehicle" to the console.
Step 4:Define the Car class that is derived from both the Vehicle and FourWheeler classes.
Step 5:In the Car class, define a constructor that prints the message "This vehicle is a 4
wheeldrive Car" to the console.
Step 6:In the main function, create an object of the Car class.
Step 7:When the Car object is created, the constructors of all the base classes (Vehicle and
FourWheeler) will be called.
Step 8:end the program
FLOWCHART:

PROGRAM:
#include
<iostream.h> using
namespace std;
// first base class
class Vehicle
{
public:
Vehicle(
)
{
cout << "This is a Vehicle" << endl;
}
};
// second base class
class FourWheeler
{
public:
FourWheeler()
{

cout << "This is a 4 wheeler Vehicle" << endl;


}
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler
{
public:
Car()
{
cout << "This vehicle is a 4 wheeldrive Car" << endl;
}
};
// main function
int main()
{

// creating object of sub class will


// invoke the constructor of base classes
Car obj;
return 0;
}
OUTPUT:
3. //C++ PROGRAM TO IMPLEMENT
MULTILEVEL INHERITANCE
ALGORITHM:
Step 1:Start the program
Step 2:Define the Vehicle class with a constructor that prints the message "This is a
Vehicle" to the console.
Step 3:Define the fourWheeler class that is derived from the Vehicle class.
Step 4:In the fourWheeler class, define a constructor that prints the message "Objects with
4 wheels are vehicles" to the console.
Step 5:Define the Car class that is derived from the fourWheeler class.
Step 6:In the Car class, define a constructor that prints the message "Car has 4 Wheels" to
the console.
Step 7:In the main function, create an object of the Car class.
Step 8:When the Car object is created, the constructors of all the base classes (Vehicle and
fourWheeler) will be called
Step 9:End the program
FLOWCHART:

PROGRAM:
#include <iostream>
using namespace std;
// base class
class
Vehicle
{
public:
Vehicle(
)
{

cout << "This is a Vehicle" << endl;


}
};
class fourWheeler: public Vehicle
{
public:
fourWheeler()
{
cout<<"Objects with 4 wheels are vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler
{
public:
Car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};
// main function
int main()
{
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
return 0;

}
OUTPUT:

4. //C++ PROGRAM TO IMPLEMENT


HIERARCHICAL INHERITANCE
ALGORITHM:
Step 1:Start the program
Step 2:Define the Vehicle class with a constructor that prints the message "This is a
Vehicle" to the console.
Step 3:Define the Car class that is derived from the Vehicle class.
Step 4:In the Car class, define a constructor that prints the message "This is a Car" to the
console.
Step 5:Define the Bus class that is derived from the Vehicle class.
Step 6:In the Bus class, define a constructor that prints the message "This is a Bus" to the
console.
Step 7:In the main function, create objects of the Car and Bus classes.
Step 8:When the Car and Bus objects are created, the constructors of the Vehicle class will
be called
Step 9:End the program
FLOWCHART:

PROGRAM:
#include <iostream>
using namespace std;
// base class
class
Vehicle
{
public:
Vehicle(
)
{
cout << "This is a Vehicle" << endl;
}
};
// first sub class
class Car: public Vehicle
{
public:
Car()
{

cout << "This is a Car" << endl;


}
};
// second sub class
class Bus: public Vehicle
{
public:
Bus()
{
cout << "This is a Bus" << endl;
}
};
// main function
int main()
{

// creating object of sub class will


// invoke the constructor of base class
Car obj1;
Bus obj2;

return 0;
}
OUTPUT:

5. //C++ PROGRAM FOR HYBRID INHERITANCE


ALGORITHM:
Step 1:start the program
Step 2:Define the Vehicle class with a constructor that prints the message "This is a
Vehicle" to the console.
Step 3:Define the Fare class with a constructor that prints the message "Fare of Vehicle\n"
to the console.
Step 4:Define the Car class that is derived from the Vehicle class.
Step 5:In the Car class, define a constructor that prints the message "Driving my car" to the
console.
Step 6:Define the Bus class that is derived from both the Vehicle and Fare classes.
Step 7:In the Bus class, define a constructor that prints the message "Riding the bus" to the
console.
Step 8:In the main function, create an object of the Bus class.
Step 9:When the Bus object is created, the constructors of both the Vehicle and Fare classes
will be called.
Step 10:end the program
FLOWCHART:

PROGRAM:
#include
<iostream.h> using
namespace std;
// base class
class
Vehicle
{
public:
vehicle()
{

cout << "This is a Vehicle" << endl;


}
};
//base class
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehicle\n";
}
};
// first child class - only inherits from Vehicle
class Car: public Vehicle
{

public:
Car()
{

cout << "Driving my car" << endl;


}
};
// second child class - inherits from BOTH Vehicle and
Fare class Bus: public Vehicle, public Fare
{
public:
Bus()
{

cout << "Riding the bus" << endl;


}
};
// main function
int main()
{
// creating object of the chcild class will
// invoke the constructor of base class - BOTH Vehicle and Fare
Bus obj2;
// creating a car object would NOT

// invoke the constructor for Fare


return 0;
}
OUTPUT:
10.PROGRAMS USING OPERATOR AND FUNCTION
OVERLOADING IN C++
1. FUNCTION OVERLOADING IN C++
ALGORITHM:
Step 1:Initialize the Addition class.
Step 2:Create an object of the Addition class.
Step 3:Call the sum() method with two arguments and print the result.
Step 4:Call the sum() method with three arguments and print the result.
Step 5:Return from the main() method.
FLOWCHART:

START

CreateobjectofAddition

ObjectoftheAdditionclass

Callsum(intnum1,intnu
m2)

Intnum1+intnum2

Displaytheresult

Callsum(intnum1,int
num2,int num3)
Intnum1+intnum2+int
num3

Displaytheresult

STOP

PROGRAM:
#include
<iostream.h> using
namespace std; class
Addition
{

public:
int sum(int num1, int num2)
{ return num1+num2;
}

int sum(int num1, int num2, int num3)


{ return num1+num2+num3;
}
};
int main(void)
{ Addition obj;
cout<<obj.sum(20,
15)<<endl;
cout<<obj.sum(81, 100, 10);
return 0;
}
OUTPUT:

2.OPERATOR OVERLOADING IN C++


ALGORITHM:
Step 1:Start the program
Step 2:Create a class named "Test" with two private and Step 3:one public member variables
Step 4:Define a default constructor for the class "Test" and initialize the private variable
"num" to 8
Step 5:Define a non-member function "void operator ++()" as a prefix increment operator
for the class "Test"
Step 5:Increment the value of the private variable "num" by 2
Step 6:Define a member function "void Print()" for the class "Test" to display the value of the
private variable "num"
Step 7:In the main function, create an object of the class "Test"
Step 8:Increment the value of the object "tt" using the prefix increment operator
Step 9:Call the "Print()" function to display the value of the private variable "num"
Step 10:End the program by returning 0 from the main function
FLOWCHART:

PROGRAM:
#include
<iostream.h> using
namespace std; class
Test
{

private:
int
num;
public:
Test(): num(8){}
void operator ++()
{ num = num+2;
}

void Print() {
cout<<"The Count is:"<<num;
}
};

int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
OUTPUT:

11.LAB PROGRAM USING VIRTUAL FUNCTIONS


ALGORITHM:
Step 1:start the program. Define an abstract class Base with a pure virtual function fun() and
a protected member variable x.
Step 2: Implement a constructor for the Base class that initializes the x member variable.
Step 3:Output a message indicating the constructor of the Base class is called.
Step 4:Define a derived class Derived that inherits from the Base class.
Step 5: Derived class adds an additional member variable y.
Step 6:Implement a constructor for the Derived class that initializes both x and y using the
constructor of the Base class.
Step 7:Implement the fun() function in the Derived class to output the values of x and y.
Step 8: In the main() function:
- Create an object d of the Derived class with values 4 and 5.

- Call the fun() function on the d object.


- Create an object ptr of type Base pointer pointing to a Derived object with values 6 and 7.
- Call the fun() function using the pointer to demonstrate
polymorphism. Step 9: end the program
FLOWCHART:

PROGRAM:
#include
<iostream.h> using
namespace std;
// An abstract class with constructor
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i) {
x = i;
cout<<"Constructor of base called\n";
}
};
class Derived: public Base
{
int
y;
public:
Derived(int i, int j):Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " <<y<<'\n';
}
};
int main(void)
{
Derived d(4, 5);
d.fun();
//object creation using pointer of base class
Base *ptr=new Derived(6,7);
ptr-
>fun();
return 0;
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy