KEMBAR78
9-16 Program Lab Exercise | PDF | Software | Computer Engineering
0% found this document useful (0 votes)
21 views20 pages

9-16 Program Lab Exercise

The document contains multiple C++ programming examples demonstrating various inheritance types including simple, multiple, hybrid, multilevel, and hierarchical inheritance. It also covers virtual functions, file manipulation, command line arguments, class templates, function templates, and exception handling. Each section includes the aim and source code for the respective C++ program.

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)
21 views20 pages

9-16 Program Lab Exercise

The document contains multiple C++ programming examples demonstrating various inheritance types including simple, multiple, hybrid, multilevel, and hierarchical inheritance. It also covers virtual functions, file manipulation, command line arguments, class templates, function templates, and exception handling. Each section includes the aim and source code for the respective C++ program.

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/ 20

9.

(A)Simple inheritance
Aim :
To write a C++ Program to demonstrate the Simple inheritance.

Source code:

#include <iostream>
using namespace std;

class A {
private:
int a;

protected:
int x; // Can access by the derived class
public:
void setVal(int v)
{
x = v;
}
};

class B : private A {
public:
void printVal(void)
{
setVal(10); // accessing public member function here
// protected data member direct access here
cout << "value of x: " << x << endl;
}
};

int main()
{
B objB; // derived class creation
objB.printVal();
return 0;
}
Output:
9(B) Multiple Inheritance.

Aim:
To write a C++ Program to demonstrate the Multiple Inheritance.

Source code:
#include<iostream>
using namespace std;

class M
{
protected:
int m;
public :
void get_M(int );
};
class N
{
protected:
int n;
public:
void get_N(int);
};
class P: public M, public N
{
public:
void display(void);
};

void M::get_M(int x)
{
m=x;
}
void N::get_N(int y)
{
n=y;
}
void P::display(void)
{
cout<<"\n\tm = "<<m<<endl;
cout<<"\n\tn = "<<n<<endl;
cout<<"\n\tm*n = "<<m*n<<endl;
}
int main()
{
P p;
p.get_M(10);
p.get_N(20);
p.display();
return 0;
}

Output:

9(C) Hybrid Inheritance

Aim:
To write a C++ Program to demonstrate the Hybrid Inheritance.

Source code:

#include<iostream>
using namespace std;
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"Roll no :: "<<rno<<"\n";
}
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained :\n"<<"part1 = "<<part1<<"\n"<<"part2 = "<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"Sports : "<<score<<"\n";

}
};

class result: public test, public sports


{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total Score = "<<total<<"\n";
}
int main()
{

result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0;
}

Output:

9(D) Multilevel Inheritance


Aim
To write a C++ Program to demonstrate the multilevel Inheritance *

#include <bits/stdc++.h>
using namespace std;

// single base class


class A {
public:
int a;
void get_A_data()
{
cout << "Enter value of a: ";
cin >> a;
}
};

// derived class from base class


class B : public A {
public:
int b;
void get_B_data()
{
cout << "Enter value of b: ";
cin >> b;
}
};

// derived from class derive1


class C : public B {
private:
int c;

public:
void get_C_data()
{
cout << "Enter value of c: ";
cin >> c;
}

// function to print sum


void sum()
{
int ans = a + b + c;
cout << "sum: " << ans;
}
};
int main()
{
// object of sub class
C obj;

obj.get_A_data();
obj.get_B_data();
obj.get_C_data();
obj.sum();
return 0;
}

9(E) Hierarchical Inheritance

Aim:

To write a C++ program to demonstrate the Hierarchical Inheritance.

Source code:

#include<iostream>

using namespace std;


class A //superclass A

public:

void show_A() {

cout<<"class A"<<endl;

};

class B : public A //subclass B

public:

void show_B() {

cout<<"class B"<<endl;

};

class C : public A //subclass C

public:

void show_C() {

cout<<"class C"<<endl;

};

int main() {

B b; // b is object of class B

cout<<"calling from B: "<<endl;

b.show_B();
b.show_A();

C c; // c is object of class C

cout<<"calling from C: "<<endl;

c.show_C();

c.show_A();

return 0;

output

10.VIRTUAL FUNCTION

AIM :

TO Write a C++ program to demonstrate Virtual Functions.


Source code
#include <iostream>

using namespace std;

class Base

public:

virtual void show() = 0;

};

class Derived : public Base

public:

void show()

std::cout << "Derived class is derived from the base class." << std::endl;

};

int main()

Base *bptr;

//Base b;

Derived d;

bptr = &d;

bptr->show();

return 0;

Output:
11. Text File

Aim :

To Write a C++ program to manipulate a Text File.

Source code:

//C++ program to write and read text in/from file.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream file; //object of fstream class

//opening file "sample.txt" in out(write) mode


file.open("sample.txt",ios::out);

if(!file)
{
cout<<"Error in creating file!!!"<<endl;
return 0;
}

cout<<"File created successfully."<<endl;


//write text into file
file<<"FILE ADDED THE CONTENT.";
//closing the file
file.close();
//again open file in read mode
file.open("sample.txt",ios::in);

if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return 0;
}

//read untill end of file is not found.


char ch; //to read single character
cout<<"File content: ";

while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}

file.close(); //close file

return 0;
}
Output:

13. Command Line Arguments


Aim
To Write a C++ program to find the Biggest Number using Command Line Arguments

Source code:

#include <iostream>
#include <cstdlib> // For atoi function

int main(int argc, char* argv[]) {


if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <number1> <number2> ... <numberN>\n";
return 1;
}

int highest = std::atoi(argv[1]); // Initialize with the first argument

for (int i = 2; i < argc; ++i) {


int currentNumber = std::atoi(argv[i]);
if (currentNumber > highest) {
highest = currentNumber;
}
}

std::cout << "The largest number is: " << highest << "\n";
return 0;
}
14. Class Template

Aim:

To Write a C++ program to demonstrate Class Template.

Source code:

#include<iostream>
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;

}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}
Output:
15. Function template
Aim:
To Write a C++ program to demonstrate Function Template.
Source code:

#include <iostream>
using namespace std;

template <typename T>


void func_swap(T &arg1, T &arg2)
{
T temp;
temp = arg1;
arg1 = arg2;
arg2 = temp;
}

int main()
{
int num1 = 10, num2 = 20;
double d1 = 100.53, d2 = 435.54;
char ch1 = 'A', ch2 = 'Z';

cout << "Original data\n";


cout << "num1 = " << num1 << "\tnum2 = " << num2<<endl;
cout << "d1 = " << d1 << "\td2 = " << d2<<endl;
cout << "ch1 = " << ch1 << "\t\tch2 = " << ch2<<endl;

func_swap(num1, num2);
func_swap(d1, d2);
func_swap(ch1, ch2);

cout << "\n\nData after swapping\n";


cout << "num1 = " << num1 << "\tnum2 = " << num2<<endl;
cout << "d1 = " << d1 << "\td2 = " << d2<<endl;
cout << "ch1 = " << ch1 << "\t\tch2 = " << ch2<<endl;

return 0;
}
Output:
16. Exception handling
Aim
To Write a C++ program to demonstrate Exception Handling.
Source code:

#include <iostream>
using namespace std;

double division(int a, int b) {


if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}

int main () {
int x = 50;
int y = 0;
double z = 0;

try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}

return 0;
}
Output:

You might also like