[go: up one dir, main page]

0% found this document useful (0 votes)
4 views16 pages

Lab With Solution

The document contains a series of C++ programming exercises that demonstrate various algorithms and programming concepts, including calculating the sum of digits, reversing a number, finding factorials, generating Fibonacci series, checking for Armstrong and prime numbers, and identifying palindromes and perfect numbers. Each exercise includes the program name, compiler instructions, and the complete source code. Additionally, it covers function overloading and matrix diagonal printing.

Uploaded by

Hrishika
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)
4 views16 pages

Lab With Solution

The document contains a series of C++ programming exercises that demonstrate various algorithms and programming concepts, including calculating the sum of digits, reversing a number, finding factorials, generating Fibonacci series, checking for Armstrong and prime numbers, and identifying palindromes and perfect numbers. Each exercise includes the program name, compiler instructions, and the complete source code. Additionally, it covers function overloading and matrix diagonal printing.

Uploaded by

Hrishika
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/ 16

COR2C5 - Computer Programming in C++ LAB

1.1 Sum of digit

// Program name: sum_of_digit.cpp


// compiler : g++ under Linux
// g++ -Wall sum_of_digit.cpp -o sum_of_digit
// run: ./sum_of_digit
/* sum of digit */

#include<iostream>
using namespace std;

int main( ) {
int s,n,n1;
s=0; //initialise s to zero
cout <<"\n enter the number : ";

cin >>n; //Read n

cout <<"the given number is "<<n;

do {
n1=n%10; // get remainder
s=s+n1; // add remainder to sum
n=n/10; // get quotient to reduce number by one digit
} while (n!=0); //check no of digit, if zero exit loop

cout <<" sum of digit is " <<s; //print sum


}

1.2 Reverse the Number

// Program name: digi_revers.cpp


// compiler : g++ under Linux
// g++ -Wall digi_revers.cpp -o digi_revers
// run: ./digi_revers
/* ex1.2.2 reverse the number */

#include <iostream>

using namespace std;

int main( ) {

int r, n;

cout <<"\n\t Enter the number : ";


cin >>n;

cout <<"\n\t Reversed number is ";

do {
r=n%10; // get remainder
n=n/10; //reduce digit by one digit
cout <<r; //print remainder
} while(n!=0);

cout << "\n";


} //end of program

LAB ASSIGNMENT Page 1


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

1.3 Factorial of a Number

// Program name: factorial.cpp


// compiler : g++ under Linux
// g++ -Wall factorial.cpp -o factorial
// run: ./factorial
/* ex1.3 factorial of a number */

#include <iostream>

using namespace std;

int main( ) {
int i,f,n;
f=1; // initialization
cout <<"\n Program will find factorial of given Number";
cout <<"\n\t enter the number : ";
cin >>n; //read input

for(i=1; i<=n; i++)


f=f*i; // find factorial using repetitively
// multiplication
cout <<"\factorial of number is "<<f; //print factorial
} //end of program

1.4 Fibonacci Series

// Program name: Fib_Series.cpp


// compiler : g++ under Linux
// g++ -Wall Fib_Series.cpp -o Fib_Series
// run: ./Fib_Series
/*
ex1.4 Fibonacci series using while loop
*/

#include <iostream>
using namespace std;

int main() {
int n, first = 0, second = 1, next;
cout << "\n\t Enter the number of terms: ";
cin >> n;
cout << "\t Fibonacci Seq.: " << first << " " << second << " ";
int i = 2;

while (i < n) {
next = first + second;
first = second;
second = next;
cout << next << " ";
i++;
}
return 0;
} // end of program

// Program name: Fib_Series.cpp


// compiler : g++ under Linux
// g++ -Wall Fib_Series.cpp -o Fib_Series
// run: ./Fib_Series
/*
ex1.4 Fibonacci series using for loop
*/

LAB ASSIGNMENT Page 2


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

#include <iostream>
using namespace std;

int main() {
int n, first = 0, second = 1, next;
cout << "\n\t Enter the number of terms: ";
cin >> n;
cout << "\t Fibonacci Seq.: " << first << " " << second << " ";

for (int i = 2, i < n, i++) {


next = first + second;
first = second;
second = next;
cout << next << " ";
}
return 0;
} // end of program

// Program name: Fib_Series.cpp


// compiler : g++ under Linux
// g++ -Wall Fib_Series.cpp -o Fib_Series
// run: ./Fib_Series
/*
ex1.4 Fibonacci series using function
*/

#include <iostream>
using namespace std;

// user define function


int fibSeries( int n)
{ int first = 0, second = 1, next;
cout << "\t Fibonacci sequence: " << first << " " << second << " ";
for (int i = 2; i < n; i++) {
next = first + second;
first = second;
second = next;
cout << next << " ";
}
} // end of function fibSeries( )

//main function
int main() {
int n, first = 0, second = 1, next;
cout << "\n\t Enter the number of terms: ";
cin >> n;
fibSeries(n);
return 0;
} // end of program

// Program name: Fib_Series.cpp


// compiler : g++ under Linux
// g++ -Wall Fib_Series.cpp -o Fib_Series
// run: ./Fib_Series
/*
ex1.4 Fibonacci series using recursion
*/

#include <iostream>
using namespace std;

LAB ASSIGNMENT Page 3


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

// user define function


void fib_recurcive (int n) {
static int first=0, second=1, next;
if(n-2 > 0) { // comparison must be n-2 times
next = first + second;
first = second;
second = next;
cout << next <<" ";
fib_recurcive(n-1);
} //end if
} // end of function fib( )

//main function
int main() {
int n ;
cout << "\n\t Enter the number of terms: ";
cin >> n;

cout << "\n\t Fibonnaci Series : ";


// Ptint fib series using for loop as follow
cout << "0 "<< "1 "; // print first two term
fib_recurcive (n); // print remaining terms
cout << endl;
return 0;
} // end of program

// Program name: Fib_Series.cpp


// compiler : g++ under Linux
// g++ -Wall Fib_Series.cpp -o Fib_Series
// run: ./Fib_Series
/*
ex1.4 Fibonacci series using recursion
*/

#include <iostream>
using namespace std;

// user define function


int fib (int n) {

if((n == 1) || (n == 0))
return(n);
else
return(fib(n-1) + fib(n-2));
} // end of function fib( )

//main function
int main() {
int n ;
cout << "\n\t Enter the number of terms: ";
cin >> n;

cout << "\n\t Fibonacci Series : ";

// Print fib series using for loop as follow


for (int i = 0; i < n; i++) {
cout << " " << fib(i);
}
/*
// Print fib series using while loop as follow
int i = 0;
while(i < n) {

LAB ASSIGNMENT Page 4


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

cout << " " << fib(i);


i++;
}
*/
cout << endl;
return 0;
} // end of program

1.5 Armstrong Number checking

// Program name: ArmstrongNumber.cpp


// compiler : g under Linux
// g -Wall ArmstrongNumber.cpp -o ArmstrongNumber
// run: ./ArmstrongNumber
/* ex1.5 Armstrong number */

#include <iostream>
#include <cmath> // to use pow(base, exp)

using namespace std;

int main( ) {
int sum = 0, num, n1 = 0, rem= 0, len = 0;
cout << "\n\t Enter the number : ";
cin >> num; //read input to n
n1=num; // make copy of input number

while (n1) {
len++; //count digits
n1=n1/10;
} // end while

n1=num; // make copy of input number again

do {
rem = num % 10; // separate LSD
num = num / 10; // reduce digit by one
sum = sum + pow (rem, len); // get sum after taking power of number
// raise to no of digit
} while (num != 0); //test that all digit has been exhausted

if(sum == n1) //check for Armstrong number


cout << "\n\t Given number " << n1 << " is Armstrong\n";
else
cout << "\n\t Given number " << n1 << " is not Armstrong\n";
} //end of program

1.6 Prime Number checking

// Program name: PrimeNumber.cpp


// compiler : g++ under Linux
// g++ -Wall PrimeNumber.cpp -o PrimeNumber
// run: ./PrimeNumber
/*
ex1.6 prime number
*/
#include <iostream>

using namespace std;

int main( ) {
int l, i, j, k, n;

LAB ASSIGNMENT Page 5


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

l=0; //l is flag set to true i.e., 0


system("clear"); //clear screen in Linux
// system("cls"); //clear screen in Windows
cout <<"\n\t enter the number : ";
cin >>n; //read input

for(i = 1; i <= n; i++) {


for(j = 2; j <= (i - 1); j++) {
k = i % j; // get remainder

if (k == 0) //if true then set flag variable to fails


l = 1;
else // else not prime
l = l;
} //endfor

if (l==0) //test for prime


cout << "\t\t" <<n <<" prime \n";
else
cout << "\t\t" <<i <<" not prime \n";

l=0;
} //endfor
} //end of program

1.7 Palindrome Checking

// Program name: Palindrome.cpp


// compiler : g++ under Linux
// g++ -Wall Palindrome.cpp -o Palindrome
// run: ./Palindrome
/*
ex1.7 palindrome or not using do ... while
*/
#include <iostream>

using namespace std;

int main( ) {
int num,n1=0,sum=0,rem=0;

cout << "\n\t Enter the number : ";


cin >> num; //read input
n1 = num; //preserve input
sum = 0;

do {
rem = num % 10; // find remainder
num = num / 10; // separate rhs digit
sum = sum * 10 + rem; // regenerate the number
} while(num != 0);

if(sum == n1)
cout << "\n\t The given number is " << n1 << " palindrome ";
else
cout << "\n\t The given number is " << n1 << " not palindrome ";
return 0;
} //end of program

1.8 Perfect Number checking.

// Program name: PerfectNumber.cpp


// compiler : g++ under Linux

LAB ASSIGNMENT Page 6


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

// g++ -Wall PerfectNumber.cpp -o PerfectNumber


// run: ./PerfectNumber
/*
ex1.2.9 Perfect number Checking
*/
#include <iostream>

using namespace std;

int main( ) {
int fact=0, num;
cout << "\n\t Enter the number : ";
cin >> num; //read input

for(int i = 1; i < num; i++) {


if ( (num % i) == 0 ) //test for remainder
fact = fact + i;
}

if (fact == num)
cout << "\n\t Number " << num << " is Perfect ";
else
cout << "\n\t Number " << num << " is not perfect";
} //end of program

1.9 write a C++ program to demonstrate simple function overloading

//ex1.9_functionOverloading.cpp
/*
C++ program to find area of square, rectangle, circle and triangle by using
function overloading
*/
#include<iostream>
using namespace std;
//user define functions
int area(int s) {
return(s*s); // return area of square
}

int area(int l,int b) {


return(l * b); // return area of rectangle
}

float area(float r) {
return(3.14 * r * r); // return area of circle
}
float area(float bs, float ht) {
return((bs * ht)/2); // return area of triangle
}
//main function
int main() {
int s, l, b;
float r, bs, ht;
cout << "Enter side of a square:";
cin >> s;
cout << "Enter length and breadth of rectangle:";
cin >> l >> b;
cout << "Enter radius of circle:";
cin >> r;
cout << "Enter base and height of triangle:";
cin >> bs >> ht;
cout << "\n Area of square is " << area(s);
cout << "\n Area of rectangle is " << area(l, b);

LAB ASSIGNMENT Page 7


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

cout << "\n Area of circle is " << area(r);


cout << "\n Area of triangle is " << area(bs, ht);
} // end of program
2.1 Find Summation of N numbers.

// Program name: Sum_of_series.cpp


// compiler : g++ under Linux
// g++ -Wall Sum_of_series.cpp -o Sum_of_series
// run: ./Sum_of_series

/* Summation of 'n' number for table of 3


use sum = sum + 3 * i ()
*/

#include <iostream>
using namespace std;

int main() {
int num, sum = 0;
cout << "\n\t Enter the number of terms : ";
cin >> num;

for(int i = 1; i <= num; i++)


sum = sum + (3 * i); // generate sum of successive number

cout << "\n\t Summation of " << num << " term is " << sum;
}

2.2 Print the Principal Diagonal and Secondary Diagonal

// MatrixDiagonals.cpp
// Program name: MatrixDiagonals.cpp
// compiler : g++ under Linux
// g++ -Wall MatrixDiagonals.cpp -o MatrixDiagonals
// run: ./MatrixDiagonals
// C++ Program to print the Diagonals of a Matrix

#include <iostream>
using namespace std;

const int COL = 10; // macro

// Function to print the Principal Diagonal


void printPrincipalDiagonal(int mat[][COL], int n) {
cout << "\n\t Print Principal Diagonal: ";

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {

// Condition for principal diagonal


if (i == j)
cout << mat[i][j] << ", ";
} // end for (inner)
} // end for (outer)
cout << endl;
} // enf of function printPrincipalDiagonal

// Function to print the Secondary Diagonal


void printSecondaryDiagonal(int mat[][COL], int n) {
cout << "\n\t Print Secondary Diagonal: ";

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {

LAB ASSIGNMENT Page 8


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

// Condition for secondary diagonal


if ((i + j) == (n - 1))
cout << mat[i][j] << ", ";
} // end for (inner)
} // end for (outer)
cout << endl;
} // enf of function printSecondaryDiagonal

// main program
int main() {
int row = 4;
int arry[][COL] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 1, 2, 3 },
{ 4, 5, 6, 7 }
};

printPrincipalDiagonal(arry, row);
printSecondaryDiagonal(arry, row);
return 0;
} // end of program

2.3 Print the Sum of two 2D Matrix

// MatrixAdd.cpp
// Program name: MatrixAdd.cpp
// compiler : g++ under Linux
// g++ -Wall MatrixAdd.cpp -o MatrixAdd
// run: ./MatrixAdd
// C++ program for addition of two matrices
#include <iostream>
using namespace std;
#define ROW 4
#define COL 4

// This function adds A[][] and B[][],


// and stores the result in C[][]
void add(int A[][COL], int B[][COL], int C[][COL]) {
int i, j;
for (i = 0; i < COL; i++)
for (j = 0; j < COL; j++)
C[i][j] = A[i][j] + B[i][j];
}

// main program
int main() {
int A[ROW][COL] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 }
};

int B[ROW][COL] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 }
};

// To store the result


int C[ROW][COL];
int i, j;
add(A, B, C);

LAB ASSIGNMENT Page 9


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

cout << "Result matrix is " << endl;


for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++)
cout << C[i][j] << " ";

cout << endl;


} // end for

return 0;
} // end of program

Write a program to manipulate the string as given :

3.1 String copy

// Program Name: str_cpy.cpp


// compiler : g++ under Linux
// g++ -Wall str_cpy.cpp -o str_cpy
// run: ./str_cpy
// 4.1. copying string using character array

#include <iostream>
#include <cstring > // or we can use <string.h> also
using namespace std;
int main ( ) {
char s1[25], s2 [25]; // character array
// string s1, s2; // string variable

cout << "Enter a string: " ;


cin >> s1; // read string
// s2=s1; // use when s1, s2 is type string
strcpy(s2, s1); // call string copy function,
// if s1, s2 is of type char
cout << "strcpy (s2, s1) :" << s2;
} // end of program

3.2 String concatenation

// Program Name : str_cat.cpp


// compiler : g++ under Linux
// g++ -Wall str_cat.cpp -o str_cat
// run: ./str_cat
// Ex 4.2. string concatenation using character array

#include<iostream>
#include<cstring>

using namespace std;

int main ( ) {
char s1[40], s2 [25]; // character array
// string s1, s2; // does not require <cstring>
cout << "\n\t string concatenation using character array \n\n";
cout << "\t Enter string s1 : ";
cin >> s1;
cout << "\t Enter string s2 : ";
cin >> s2;
strcat(s1, s2);
cout << "\t strcat (s1, s2 ) : " << s1;
// cout << "strcat (s1, s2 ) : " << s1 = s2;
} //end of program

LAB ASSIGNMENT Page 10


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

3.3 String Comparison

// Program Name: str_cmp.cpp:


// compiler : g++ under Linux
// g++ -Wall str_cmp.cpp -o str_cmp
// run: ./str_cmp
// ex4.3 string comparison
#include<iostream>
#include<cstring>
using namespace std;

int main ( ) {
char s1[25], s2 [25];
cout << "\n\t String Comparison \n\n";
cout << "\t Enter string s1 : " ;
cin >> s1;
cout << "\t Enter string s2 : ";
cin >> s2;
// compare str1 and str2 lexicographically
int status = strcmp ( s1, s2 ); // store comparison status
cout << "\n strcmp (s1, s2 ): ";

switch(strcmp (s1, s2 )) {
case -1 :
cout << s1 << " is less then " << s2;
break;

case 0 :
cout << s1 << " is equal to " << s2;
break;

case 1 :
cout << s1 << " is grater then " << s2;
break;
} // end of switch()
} // end of program

3.4 String reverse

// Program Name: Rev_str.cpp


// compiler : g++ under Linux
// g++ -Wall Rev_str.cpp -o Rev_str
// run: ./Rev_str
// ex4.4 Reverse string
#include <iostream>
#include <cstring>
using namespace std;

int main ( ) {
char str[25];
cout << "\n\t Reverse String \n\n" ;
cout << "\t Enter a string :" ;
cin >> str;
// cout << "\t strrev (str) : " << strrev(str); // or use following
// to lines
strrev(str);
cout << "\t strrev (str) : " << str;
}

4.1 Give a suitable example for Single inheritance.

LAB ASSIGNMENT Page 11


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

// SingleInheritance01.cpp
// Example 8.1: Protected data member and accessing methods
// without arguments

#include<iostream>
using namespace std;

class A {
protected:
int a; // data members of class A

public:
void set_A() {
cout << "Enter the Value of a for class A = ";
cin >> a; // read value of a from KB
}

void disp_A() {
cout << endl << "Value of a for class A = " << a;
}
}; //end of class A (base class)

class B: public A {
int b, p; // data members of class A

public:
void set_B() {
set_A(); // call method from base class A
cout << "Enter the Value of b for class B = ";
cin >> b; // read value of a from KB
}

void disp_B() {
disp_A(); // call method from base class A
cout << endl << "Value of b of class B = " << b;
}

void cal_product() {
p = a * b;
cout << endl << "Product of " << a << " * "
<< b << " = " << p;
}
}; //end of sub class B

// main function
main() {
B obj_b; // drive class object
obj_b.set_B(); // call method of drive class
obj_b.cal_product(); // call method of drive class

return 0;
}

4.2 Give a suitable example for multiple Inheritance.

// MultipleInheritance.cpp
// Example 8.2 : C++ program to explain multiple inheritance

#include <iostream>
using namespace std;

// first base class


class Vehicle {

LAB ASSIGNMENT Page 12


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

public:
Vehicle() {
cout << "Executing constructor Vehicle\n";
}
}; // end of class Vehicle

// second base class


class FourWheeler {
public:
FourWheeler() {
cout << "Executing Constructor 4 Wheeler Vehicle\n";
}
}; // class FourWheeler (base)

// sub class derived from two base classes


class Car : public Vehicle, public FourWheeler {

}; //

// main function
int main() {
// Creating object of sub class will invoke
// the constructor of base classes.
Vehicle obj_V;
cout << endl;
// Car obj_C;
FourWheeler obj_FW;
cout << endl;
Car obj_C;

return 0;
}

4.3 Give a suitable example for multi level Inheritance.

// MultilevelInheritance.cpp
// example 11: C++ program to implement Multilevel Inheritance
#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() {
cout << "\t Executing constructor Vehicle\n";
}
}; //end of base class Vehicle

// first sub_class derived from class vehicle


class fourWheeler : public Vehicle {
public:
fourWheeler() {
cout << "\t Executing Constructor fourWheeler\n";
}
};

// sub class derived from the derived base class fourWheeler


class Car : public fourWheeler {
public:
Car() {
cout << "\t Executing Constructor Car\n";

LAB ASSIGNMENT Page 13


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

}
};

// main function
int main() {
// Creating object of sub class will invoke
// the constructor of base classes.
Vehicle obj_V;
cout << endl;
fourWheeler obj_fw;
cout << endl;
Car obj;
cout << endl;
return 0;
} //end of program

4.4 Give a suitable example for Hybrid Inheritance.

// HybridInheritance.cpp
// Example 8.2 : C++ program for Hybrid Inheritance

#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle\n";
}
};

// base class
class Fare {
public:
Fare() {
cout << "Fare of Vehicle\n";
}
};

// first sub class


class Car : public Vehicle {
// no member in this class
};

// second sub class


class Bus : public Vehicle, public Fare {
// no member in this class
};

// main function
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
} //end of program

4.5 Give a suitable example for Hierarchical Inheritance.

// HierarchicalInheritance.cpp
// Example 12: C++ program to implement Hierarchical Inheritance

LAB ASSIGNMENT Page 14


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() {
cout << "\n\t Executing constructor Vehicle\n";
}
}; //end of class Vehicle

// first sub class


class Car : public Vehicle {
//no member in this class
}; //end of sub class Car

// second sub class


class Bus : public Vehicle {
//no member in this class
}; //end of sub class Bus

// main function
int main() {
// creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}

4.6 Write a program to overload + operator.


Write a program to demonstrate friend function
Write a program to demonstrate compile-time polymerise binding (early binding)
Write a program to demonstrate parameterised constructor.

//EXP 17.1 Complex Number addition operator overloading


// and friend function
//ComplexAdditionOperatorOverloading.cpp
#include<iostream.h>
using namespace std;

class complex {
public:
int real;
int img;
complex() { // constructor
real = img = 0;
}

complex(int x,int y) { // constructor with parameter


real = x;
img = y;
}

void show() {
cout<< "\n" << real << "+" << im << "i";
}
// friend function and over loading function declaration
friend complex operator+(complex c, complex d);
}; //end of class complex

// operator+ is not part of complex class so have

LAB ASSIGNMENT Page 15


(NAVANIT CHOUDHURY)
COR2C5 - Computer Programming in C++ LAB

// 2 args for + operator overload.


complex operator+(complex c, complex f) {
complex ans;
ans.real = c.real+f.real;
ans.img = c.img+f.img;
return(ans);
}

// main function
int main() {
complex x(1, 2), y(0, 7);
complex c = x + y; //overloaded + is called here
c.show();
} // end of main

4.7 Write a program to demonstrate virtual function overriding

// VirtualFunctionOverriding.cpp
// example 05 : C++ program for virtual function overriding
#include <bits/stdc++.h>
using namespace std;

class base {
public:
virtual void print() {
cout << "print base class" << endl;
}

void show() {
cout << "show base class" << endl;
}
}; // end of base class

class derived : public base {


public:
// print () is already virtual function in
// derived class, we could also declared as
// virtual void print () explicitly
void print() {
cout << "print derived class" << endl;
}

void show() {
cout << "show derived class" << endl;
}
}; // end of drive class

// main function
int main() {
base* bptr; // object pointer
derived d; // object
bptr = &d; // assign address od drived object to base pointer
// Virtual function, binded at runtime (Runtime polymorphism)
bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

return 0;
}

LAB ASSIGNMENT Page 16


(NAVANIT CHOUDHURY)

You might also like