0% found this document useful (0 votes)
11 views15 pages

Practical Prints of C++

Uploaded by

tiku024iii
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)
11 views15 pages

Practical Prints of C++

Uploaded by

tiku024iii
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/ 15

Prac cal No-1

/* Progrm to check given number is even or odd */


#include <iostream>
using namespace std;

int main()
{
int num;
cout<<"Enter an integer number: ";
cin>>num;

if(num%2==0)
cout<<num<<" is an EVEN number. ";
else
cout<<num<<" is an ODD number. ";

return 0;

OUTPUT-
Enter an integer number: 10
10 is an EVEN number.
Prac cal 2. Write a program to demonstrate use of Func on overloading.

#include <iostream>
using namespace std;

int add(int, int);


float add(float, float);
int main()
{
int a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);

cout << "Sum of integers: " << x << endl;


cout << "Enter two floa ng point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
int add(int x, int y)
{
int sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}
OUTPUT:-
Enter two integers
10
30
Sum of integers: 40
Enter two floa ng point numbers
20.3
26.22
Sum of floats: 46.52
Prac cal No 3. Write a program to demonstrate encapsula on using of class.

#include<iostream>
#include<string.h>
using namespace std;
class Book{
private:
int page;
float price;
char name[40];
public:
void setPage(int p){
page = p;
}
void setPrice(float pr){
price = pr;
}
void setName(char *n){
strcpy(name,n);
}
int getPage(){
return page;
}
float getPrice(){
return price;
}
string getName(){
return name;
}
};
main(){
Book b1;
b1.setPage(200);
b1.setPrice(700.54);
b1.setName("Java Programming");
cout<<"Book 1 Page is:"<<b1.getPage()<<endl;
cout<<"Book 1 Price is:"<<b1.getPrice()<<endl;
cout<<"Book 1 Name is:"<<b1.getName()<<endl;
}
OUTPUT-
Book 1 Page is:200
Book 1 Price is:700.54
Book 1 Name is:Java Programming
Prac cal No 4. Write a program to demonstrate use constructors and Destructor

#include<iostream>
using namespace std;
class parent{
public:
parent(){
cout<<"Constructor of parent class"<<endl;
}
~parent(){
cout<<"Destructor of parent class"<<endl;
}
};
class child : public parent{
public:
child(){
cout<<"Constructor of child class"<<endl;
}
~child(){
cout<<"Destructor of child class"<<endl;
}
};
main(){
child obj;
}
OUTPUT-
Constructor of parent class
Constructor of child class
Destructor of child class
Destructor of parent class
Prac cal No 5. Write a program to demonstrate single inheritance.

#include <iostream>
using namespace std;

class Person {
int id;
char name[100];

public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
class Student : private Person {
char course[50];
int fee;

public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}

void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}

OUTPUT-
Enter the Id:1
Enter the Name:krishana
Enter the Course Name:java
Enter the Course Fee:9000

Id: 1
Name: krishana
Course: java
Fee: 9000
Prac cal No 6. Write a program to demonstrate mul ple inheritances.

#include <iostream>
using namespace std;

class Base_class
{
public:
void display()
{
cout << " It is the first func on of the Base class " << endl;
}
};
class Base_class2
{
public:
void display2()
{
cout << " It is the second func on of the Base class " << endl;
}
};
class child_class: public Base_class, public Base_class2
{
public:
void display3()
{
cout << " It is the func on of the derived class " << endl;
}

};
int main ()
{
child_class ch;
ch.display();
ch.display2();
ch.display3();
}
OUTPUT-
It is the first func on of the Base class
It is the second func on of the Base class
It is the func on of the derived class
7. Write a program to demonstrate use of operator overloading using friend func on.

#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;

public:
Complex(double r, double i) : real(r), imag(i)
{
}

// Declare the friend func on


friend Complex operator+(const Complex& c1, const Complex& c2);

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

// Define the friend func on to overload the + operator


Complex operator+(const Complex& c1, const Complex& c2)
{
double realSum = c1.real + c2.real;
double imagSum = c1.imag + c2.imag;
return Complex(realSum, imagSum);
}

int main() {
Complex num1(3.0, 2.0);
Complex num2(1.5, 4.5);

Complex sum = num1 + num2; // Operator overloading with the friend func on

cout << "Sum of two complex numbers: ";


sum.display();

return 0;
}

OUTPUT-
Sum of two complex numbers: 4.5 + 6.5i
Prac cal No-8. Write a program to demonstrate use of friend func on.

#include <iostream>
using namespace std;
class A;
class B {
int x;
public:
void set_data(int a)
{
x = a;
}
friend void max(B, A);
};
class A {
int y;
public:
void set_data(int a)
{
y = a;
}
friend void max(B, A);
};
void max(B t1, A t2)
{
if (t1.x > t2.y)
cout << t1.x;
else
cout << t2.y;
}
int main()
{
A obj1;
B obj2;
obj1.set_data(20);
obj2.set_data(35);
max(obj2, obj1); // calling friend func on
return 0;
}

OUTPUT-
35
Prac cal No-9. Write a program to demonstrate use of Virtual func ons.

#include <iostream>
using namespace std;

class base {
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
}
};
class derived : public base {
public:
void print()
{
cout << "print derived class\n";
}
void show()
{
cout << "show derived class\n";
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
}

OUTPUT-
print derived class
show base class
Prac cal No-10. Write a program to demonstrate use of pointer.
a) Write a program to demonstrate use of pointer to pointer.
b) Write a program to demonstrate use of pointer to objects.
c) Write a program to demonstrate use of pointer to func on.

//Write a program to demonstrate use of pointer to pointer.

#include <iostream>
using namespace std;
int main () {
int var;
int *ptr;
int **pptr;

var = 3000;

// take the address of var


ptr = &var;

// take the address of ptr using address of operator &


pptr = &ptr;

// take the value using pptr


cout << "Value of var :" << var << endl;
cout << "Value available at *ptr :" << *ptr << endl;
cout << "Value available at **pptr :" << **pptr << endl;
return 0;
}

OUTPUT-
Value of var :3000
Value available at *ptr :3000
Value available at **pptr :3000
//Write a program to demonstrate use of pointer to objects.

#include<iostream>
using namespace std;

class Date
{
private:
short int dd, mm, yy;

public:
Date()
{
dd = mm = yy = 0;
}
void getdata(int i, int j, int k)
{
dd = i;
mm = j;
yy = k;
}
void prndata(void)
{
cout<<"\nData is "<<dd<<"/"<<mm<<"/"<<yy<<"\n";
}
};
main()
{
Date D1;
Date *dptr;

cout<<"Ini alizing data members using the object, with values 19, 10, 2016"<<endl;
D1.getdata(19,10,2016);

cout<<"Prin ng members using the object ";


D1.prndata();

dptr = &D1;
cout<<"Prin ng members using the object pointer ";
dptr->prndata();
cout<<"\nIni alizing data members using the object pointer, with values 20, 10, 2016"<<endl;
dptr->getdata(20, 10, 2016);
cout<<"prin ng members using the object ";
D1.prndata();
cout<<"Prin ng members using the object pointer ";
dptr->prndata();
return 0;
}

OUTPUT-
Ini alizing data members using the object, with values 19, 10, 2016
Prin ng members using the object
Data is 19/10/2016
Prin ng members using the object pointer
Data is 19/10/2016

Ini alizing data members using the object pointer, with values 20, 10, 2016
prin ng members using the object
Data is 20/10/2016
Prin ng members using the object pointer
Data is 20/10/2016
//Write a program to demonstrate use of pointer to func on

#include <iostream>
using namespace std;
void printname(char *name)
{
cout << "Name is :" <<name<<endl;
}

int main()
{
char s[20]; // array declara on
void (*ptr)(char*); // func on pointer declara on
ptr=printname; // storing the address of printname in ptr.
cout << "Enter the name of the person: " <<endl;
cin>>s;
cout<<s;
ptr(s); // calling printname() func on
return 0;
}

OUTPUT-
Enter the name of the person:
Ri k
Ri kName is :Ri k
Prac cal No-11. Write a program to demonstrate use of Excep on Handling

#include<iostream>
using namespace std;
int main(){
int a,b,result;
cout<<"Enter two numbers:";
cin>>a>>b;
try{
if(b!=0)
{
result=a/b;
cout<<"Result :"<<result;
}
else throw(b);
}
catch(int number){
cout<<"Divide by zero is not allowed";
}
return(0);
}

OUTPUT-
Enter two numbers:
5
0
Divide by zero is not allowed

You might also like