Practical No.1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Practical No: 01

Q. Write a program to implement following structure-


a) if-else b) switch-break c) for loop
A) if-else
#include<iostream>
using namespace std;
int main()
{
int a; // decalre a variable
cout<<"Enter a Number = "<<endl;
cin>>a; // give input from user
if(a>10) // check first condition
{
cout<<"Your Enter number is Greater than 10";
}
else if(a==10) // check second condition
{
cout<<"Your Entered Number is equal to 10";
}
else
{
cout<<"Your Entered Number is Smaller than 10";
}
return 0;
}
Output :
B) Switch Break
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Select Your Operator: + , - : ";
cin >> op;
cout << "Enter Two Numbers: ";
cin >> num1 >> num2;
switch(op)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
default:
// If the operator is other than +, - error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output:
C) FOR LOOP
#include<iostream>
using namespace std;
int main()
{
int array[6]={1,2,3,4,5,6};
for(int i=0;i<6;i++)
{
cout<<"array number is = "<<array[i]<<endl;
}
return 0;
}

Output:
Practical No: 02
Q. Write a program to demonstrate the use of class and object.
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
// create object of Room class
Room room1;
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
// calculate and display the area and volume of the room
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output:
Practical No: 03
Q. Write a program to use of constructor and destructor
#include<iostream>
using namespace std;
class A
{
public:
A(){
cout<<"It is a constructor"<<endl;
}
~A()
{
cout<<"it is a Distructor"<<endl;
}
};
int main()
{
A obj1;
return 0;
}
Output:
Practical No: 04
Q. Write a program to Implement ‘structure’ in c++
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
}
Output:
Practical No: 05
Q. Write a program to Implement ‘Union’ in c++.
#include <stdio.h>
union Job
{
float salary;
int workerNo;
}
j;
int main()
{
j.salary = 12.3;
// when j.workerNo is assigned a value,
// j.salary will no longer hold 12.3
j.workerNo = 100;
printf("Salary = %.1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}

Output:
Practical No: 06
Q. Write a program to demonstrate single dimensional array
#include<iostream>
using namespace std;
int main()
{
int array[9]={1,2,3,4,5,6,7,8,9};
cout<<"The Array Numbers are : ";
for (int i=0;i<9;i++)
{
cout<<array[i]<<" ";
}
return 0;
}
Output:
Practical No: 07
Q. Write a program to demonstrate multi - dimensional array.
#include<iostream>
using namespace std;
int main()
{
int array[5][4]={{1,2,3,5},{3,4,5,6},{5,6,7,8},{7,8,9,10},{9,10,11,12}};
cout<<"The Array Numbers are : ";
for (int i=0;i<5;i++)
{
cout<<"\t"<<endl;
for(int j=0;j<4;j++){
cout<<array[i][j]<<"\t";
}
}
return 0;
}
Output:
Practical No: 08
Q. Write a program to perform single level inheritance.
#include<iostream>
using namespace std;
class first{
public:
int a=10;
};
class second:public first{
public:
int b=20;
int c=a+b;
};
int main()
{
second obj1;
cout<<"The addition is: "<<obj1.c<<endl;
return 0;
}
Output:
Practical No: 09
Q. Write a program to perform multilevel inheritance.
#include<iostream>
using namespace std;
class first{
public: void display1(){
cout<<"this is from Base calss"<<endl; }
};
class second:public first{
public: void display2(){
cout<<"this is from derived 1 calss"<<endl;}
};
class third:public second{
public: void display3(){
cout<<"this is from derived 2 calss"<<endl;}
};
int main()
{
third obj1;
obj1.display1();
obj1.display2();
obj1.display3();
return 0;
}
Output:
Practical No: 10
Q. Write a program to perform multiple inheritance.
#include<iostream>
using namespace std;
class first{
public: void display1(){
cout<<"this is from Base first calss"<<endl;}
};
class second{
public: void display2(){
cout<<"this is from base second calss"<<endl;}
};
class third:public second, public first{
public: void display3(){
cout<<"this is from derived from 1st and 2nd calss"<<endl; }
};
int main() {
third obj1;
obj1.display1();
obj1.display2();
obj1.display3();
return 0;
}
Output:
Practical No: 11
Q. Write a program to perform hierarchical inheritance.
#include<iostream>
using namespace std;
class first{
public:
void display1(){
cout<<"this is from Base first calss"<<endl;
}
};
class second: public first{
public:
void display2(){
cout<<"this is from first derived class"<<endl;
}
};
class third:public second{
public:
void display3(){
cout<<"this is from second derived calss"<<endl;
}
};
class fourth: public third{
public:
void display4(){
cout<<"this is from third derived calss"<<endl;
}
};
class fifth:public fourth{
public:
void display5(){
cout<<"this is from fourth derived calss"<<endl;
}
};
int main()
{
fifth obj1;
obj1.display1();
obj1.display2();
obj1.display3();
obj1.display4();
obj1.display5();
return 0;
}

Output:
Practical No: 12
Q. Write a program to perform hybrid inheritance.
#include<iostream>
using namespace std;
class first{
public:
void display1(){
cout<<"this is from Base first calss"<<endl;
}
};
class second{
public:
void display2(){
cout<<"this is from second class"<<endl;
}
};
class third:public first{
public:
void display3(){
cout<<"this is from second derived calss"<<endl;
}
};
class fourth: public third{
public:
void display4(){
cout<<"this is from third derived calss"<<endl;
}
};
class fifth:public fourth, public second{
public:
void display5(){
cout<<"this is from fourth derived calss"<<endl;
}
};
int main()
{
fifth obj1;
second obj2;
obj2.display2();
obj1.display1();
obj1.display3();
obj1.display4();
obj1.display5();
return 0;
}

Output:
Practical No: 13
Q. Write a program to overload Unary ‘-‘ operator with the help of member function.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5) {} // Constructor to initialize count to 5
void operator -() { // Overload - when used as prefix
--value;
}
void display() {
cout << "Count: " << value << endl; }
};
int main() {
Count count1;
-count1; // Call the "void operator - ()" function
count1.display();
return 0;
}
Output:
Practical No: 14
Q. Write a program to overload Binary ‘+’ operator with the help of member function.
#include <iostream>
using namespace std;
class Arith_num
{
// declare data member or variable
int x, y;
public:
// create a member function to take input
void input()
{
cout << " Enter the first number: ";
cin >> x;
}
void input2()
{
cout << " Enter the second number: ";
cin >> y;
}
// overloading the binary '+' operator to add number
Arith_num operator + (Arith_num &ob)
{
// create an object
Arith_num A;
// assign values to object
A.x = x + ob.x;
return (A);
}
// display the result of binary + operator
void print()
{
cout << "The sum of two numbers is: " <<x;
}
};
int main ()
{
Arith_num x1, y1, res; // here we create object of the class Arith_num i.e x1 and y1
// accepting the values
x1.input();
y1.input();
// assign result of x1 and x2 to res
res = x1 + y1;
// call the print() function to display the results
res.print();
return 0;
}
Output:
Practical No: 15
Q. Write a program to implement memory management operators.
#include <iostream>
using namespace std;
int main() {
// declare an int pointer
int* pointInt;
// declare a float pointer
float* pointFloat;
// dynamically allocate memory
pointInt = new int;
pointFloat = new float;
// assigning value to the memory
*pointInt = 45;
*pointFloat = 45.45f;
cout <<"First value : " << *pointInt << endl;
cout << "Float value : "<<*pointFloat << endl;
// deallocate the memory
delete pointInt;
delete pointFloat;
return 0;
}
Output:

You might also like