0% found this document useful (0 votes)
27 views

CPP Practical

Uploaded by

syedhuzaif9527
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)
27 views

CPP Practical

Uploaded by

syedhuzaif9527
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/ 19

program on dealing with array

// Name: Syed Saad


// Roll No: 16
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
for (const int &n : numbers)
{
cout << n << " ";
}
cout << "\nThe numbers are: ";
for (int i = 0; i < 5; ++i)
{
cout << numbers[i] << " ";
}
return 0;
}
Output:
The numbers are: 7 5 6 12 35
The numbers are: 7 5 6 12 35

Program on classes : string and math


// Name: Syed Saad
// Roll No: 16
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 2.3;
cout << "Sine value of x=2.3 : " << sin(x) << endl;
cout << "Cosine value of x=2.3 : " << cos(x) << endl;
cout << "Tangent value of x=2.3 : " << tan(x) << endl;
double y = 0.25;
cout << "Square root value of y=0.25 : " << sqrt(y) <<
endl;
int z = -10;
cout << "Absolute value of z=-10 : " << abs(z) << endl;
cout << "Power value: x^y = (2.3^0.25) : " << pow(x, y)
<< endl;
x = 3.0;
y = 4.0;
cout << "Hypotenuse having other two sides as x=3.0 and"
<< " y=4.0 : " << hypot(x, y) << endl;
x = 4.56;
cout << "Floor value of x=4.56 is : " << floor(x) <<
endl;
x = -4.57;
cout << "Absolute value of x=-4.57 is : " << fabs(x) <<
endl;
x = 1.0;
cout << "Arc Cosine value of x=1.0 : " << acos(x) <<
endl;
cout << "Arc Sine value of x=1.0 : " << asin(x) << endl;
cout << "Arc Tangent value of x=1.0 : " << atan(x) <<
endl;
y = 12.3;
cout << "Ceiling value of y=12.3 : " << ceil(y) << endl;
x = 57.3; // in radians
cout << "Hyperbolic Cosine of x=57.3 : " << cosh(x) <<
endl;
cout << "Hyperbolic tangent of x=57.3 : " << tanh(x) <<
endl;
y = 100.0;
// Natural base with 'e' cout << "Log value of y=100.0
is : " << log(y) << endl;
return 0;
}
Output:
Sine value of x=2.3 : 0.745705
Cosine value of x=2.3 : -0.666276
Tangent value of x=2.3 : -1.11921
Square root value of y=0.25 : 0.5
Absolute value of z=-10 : 10
Power value: x^y = (2.3^0.25) : 1.23149
Hypotenuse having other two sides as x=3.0 and y=4.0 : 5
Floor value of x=4.56 is : 4
Absolute value of x=-4.57 is : 4.57
Arc Cosine value of x=1.0 : 0
Arc Sine value of x=1.0 : 1.5708
Arc Tangent value of x=1.0 : 0.785398
Ceiling value of y=12.3 : 13
Hyperbolic Cosine of x=57.3 : 3.83746e+24
Hyperbolic tangent of x=57.3 : 1
Log value of y=100.0 is : 4.60517

Program on inheritance and polymorphism


inheritance
// Name: Syed Saad
// Roll No: 16
#include<iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();
// Calling member of the derived class
dog1.bark();
return 0;
}
Output:
i can eat!
i can sleep!
i can bark!woof woof!!

Polymorphism
// Name: Syed Saad
// Roll No: 16
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl;
}
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
// An example call to "operator+" c3.print();
}
Output:
12+i9

Programs on garbage collection, packaging, access


modifiers, as well as static and abstract modifiers.
Garbage collection
// Name: Syed Saad
// Roll No: 16
#include <iostream>
class A {
int x;
public:
A() { x = 0; ++x; }
};
int main() {
for (int i = 0; i < 1000000000; ++i) {
A *a = new A();
delete a;
}
std::cout << "DING!" << std::endl;
}
Output:
DING!
Packaging
#include<iostream>
using namespace std;
void binPacking(int *a, int size, int n) {
int binCount = 1;
int m = size;
for (int i = 0; i < n; i++) {
if (m - *(a + i) > 0) {
m -= *(a + i);
continue;
} else {
binCount++;
m = size;
i--;
}
}
cout << "Number of bins required: " << binCount;
}
int main(int argc, char **argv)
{
cout << "Enter the number of items in Set: ";
int n;
cin >> n;
cout << "Enter " << n << " items:";
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "Enter the bin size: ";
int size;
cin >> size;
binPacking(a, size, n);
}
Output:
Enter the number of items in Set: 3
Enter 3 items:4
6
7
Enter the bin size: 26
Number of bins required: 1

Access modifiers
#include<iostream>
using namespace std;
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle obj;
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
cout << "Area is: " << obj.compute_area();
return 0;
}
Output:
Radius is: 5.5
Area is: 94.985
Static modifiers
#include <bits/stdc++.h>
using namespace std;
class Base {
public : static int val;
static int func(int a) {
cout << "Static member function called";
cout << "The value of a : " << a;
}
};
int Base::val=28;
int main() {
Base b;
Base::func(8);
cout << "The static variable value : " << b.val;
return 0;
}
Output:
Static member function called
The value of a : 8The static variable value:28

Abstract modifiers
#include <iostream>
using namespace std;
class Shape {
public:
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea() {
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total Rectangle area: " << Rect.getArea() <<
endl;
Tri.setWidth(5);
Tri.setHeight(7);
cout << "Total Triangle area: " << Tri.getArea() <<
endl;
return 0;
}
Output:
Total Rectangle area: 35
Total Triangle area: 17

Program on file handling and stream


// Name: Syed Saad
// Roll No: 16
ing namespace std;
int main()
{
fstream new_file;
new_file.open("new_file",ios::out);
if(!new_file)
{
cout<<"File creation failed";
}
else
{
cout<<"New file created”;
new_file.close(); // Step 4: Closing file
}
return 0;
}
Output:
new file

Program on dynamic polymorphism


// Name: Syed Saad
// Roll No: 16
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw(){
cout<<"drawing..."<<endl;
}
};
class Rectangle: public Shape
{
public:
void draw()
{
cout<<"drawing rectangle..."<<endl;
}
};
class Circle: public Shape
{
public:
void draw()
{
cout<<"drawing circle..."<<endl;
}
};
int main(void) {
Shape *s;
Shape sh;
Rectangle rec;
Circle cir;
s=&sh;
s->draw();
s=&rec;
s->draw();
s=&cir;
s->draw();
}
Output:
drawing... drawing rectangle... drawing circle...

Programs on dynamic memory management


// Name: Syed Saad
// Roll No: 16
#include <iostream>
using namespace std;
int main ()
{
int* p = NULL;
p = new(nothrow) int;
if (!p)
cout << "allocation of memory failed\n";
else
{
*p = 29;
cout << "Value of p: " << *p << endl;
}
float *r = new float(75.25);
cout << "Value of r: " << *r << endl;
int n = 5;
int *q = new(nothrow) int[n];
if (!q)
cout << "allocation of memory failed\n";
else
{
for (int i = 0; i < n; i++)
q[i] = i+1;
cout << "Value store in block of memory: ";
for (int i = 0; i < n; i++)
cout << q[i] << " ";
}
delete p;
delete r;
delete[] q;
return 0;
}
Output:
Value of p: 29
Value of r: 75.25
Value store in block of memory: 1 2 3 4 5

Programs on exception handling


// Name: Syed Saad
// Roll No: 16
#include <iostream>
using namespace std;
int main()
{
int x = -1;
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}
cout << "After catch (Will be executed) \n";
return 0;
}
Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)
Programs on exception handling
// Name: Syed Saad
// Roll No: 16
#include <iostream>
using namespace std;
int main()
{
int x = -1;
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}
cout << "After catch (Will be executed) \n";
return 0;
}
Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)
Programs on STL containers and iterators
// Name: Syed Saad
// Roll No: 16
STL containers
#include <iostream>
#include <vector>
using namespace std;
int main() {
// initialize a vector of int type
vector<int> numbers = {1, 100, 10, 70, 100};
// print the vector
cout << "Numbers are: ";
for(auto &num: numbers) {
cout << num << ", ";
}
return 0;
}
Output:
Numbers are: 1, 100, 10, 70, 100,

Iterators

#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1 = { 10, 20, 30, 40 };
vector<int>::iterator itr;
cout << "Traversing without iterator : ";
for (int j = 0; j < 4; ++j) {
cout << v1[j] << " ";
}
cout << "\n";
cout << "Traversing using iterator ";
for (itr = v1.begin(); itr != v1.end(); ++itr) {
cout << *itr << " ";
}
cout << "\n\n";
v1.push_back(50);
cout << "Traversing without iterator : ";
for (int j = 0; j < 5; ++j) {
cout << v1[j] << " ";
}
cout << "\n";
cout << "Traversing using iterator ";
for (itr = v1.begin(); itr != v1.end(); ++itr) {
cout << *itr << " ";
}
cout << "\n\n";
return 0;
}
Output:
Traversing without iterator : 10 20 30 40
Traversing using iterator 10 20 30 40
Traversing without iterator : 10 20 30 40 50
Traversing using iterator 10 20 30 40 50

You might also like