प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 2
सामान्य निर्देश
1. प्रैक्टिकल कॉपी क
े दो भाग होंगे |
2. पहले भाग में सी प्लस प्लस क
े 10-20 प्रोग्राम ललखने होंगे साथ में आउटपुट भी
ललखना है प्रोग्राम क
े अं त में |
3. प्रोग्राम इन टॉपपक से हो |
a. Function
b. Class and object
c. Function Overloading
d. Constructor and Destructor
e. Inheritance
f. Array (Data Structure)
g. Stack, Queue and Linked List
4. दुसरे भाग SQL से 5-10 Query ललखनी है जो पकसी टेबल से सम्बन्धित हो |
भाग -१
1. C++ program to Find Sum of Natural Numbers using Recursion
#include<iostream.h>
int add(int n);
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Sum = " << add(n);
return 0;
}
int add(int n) {
if(n != 0)
return n + add(n - 1);
return 0;
}
प्रै
क्
टिकल
कॉपी
–
क
ं
प्य
ू
ि
र
साइं
स
|
झारखण्ड
बोडड
–
कक्षा
12,
YouTube
Channel:
@jharkhandeducation,
@computerkaksha
2.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 3
Output
Enter an positive integer: 10
Sum = 55
2. C++ program to Calculate Factorial of a Number Using Recursion
#include<iostream.h>
int factorial(int n);
int main( ) {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n) {
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Output
Enter an positive integer: 6
Factorial of 6 = 720
3. Object and Class in C++ Programming
// Program to illustrate the working of
// objects and class in C++ Programming
#include <iostream.h>
// create a class
class Room {
प्रै
क्
टिकल
कॉपी
–
क
ं
प्य
ू
ि
र
साइं
स
|
झारखण्ड
बोडड
–
कक्षा
12,
YouTube
Channel:
@jharkhandeducation,
@computerkaksha
3.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 4
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;
}
4.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 5
Output
Area of Room = 1309
Volume of Room = 25132.8
4. Program to demonstrate Default Constructor
// C++ program to demonstrate the use of default constructor
#include <iostream.h>
// declare a class
class Wall {
private:
double length;
public:
// default constructor to initialize variable
Wall() {
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
Wall wall1;
return 0;
}
Output
Creating a Wall
Length = 5.5
5. Program to demonstrate Parameterized Constructor
// C++ program to calculate the area of a wall
#include <iostream.h>
// declare a class
class Wall {
5.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 6
private:
double length;
double height;
public:
// parameterized constructor to initialize variables
Wall(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
Output
Area of Wall 1: 90.3
Area of Wall 2: 53.55
6. Program to demonstrate Copy Constructor
#include <iostream.h>
// declare a class
class Wall {
6.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 7
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt) {
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
7.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 8
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
Output
Area of Wall 1: 90.3
Area of Wall 2: 90.3
7. C++ program to demonstrate single inheritance
#include <iostream.h>
// 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;
}
};
8.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 9
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!!
8. C++ program to demonstrate the working of public inheritance
#include <iostream.h>
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
9.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 10
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
Output
Private = 1
Protected = 2
Public = 3
9. C++ program to demonstrate the working of protected inheritance
#include <iostream.h>
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
10.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 11
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Output
11.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 12
Private cannot be accessed.
Protected = 2
Public = 3
10. C++ program to demonstrate the working of private inheritance
#include <iostream.h>
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PrivateDerived : private Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access private member
int getPub() {
return pub;
12.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 13
}
};
int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Output
Private cannot be accessed.
Protected = 2
Public = 3
11. C++ Program to print Matrix
#include<iostream.h>
int main()
{
int arr[10][10], rows, cols, i, j;
cout<<"n Enter Rows for Array (Max 10) : ";
cin>>rows;
cout<<"n Enter Columns for Array (Max 10) : ";
cin>>cols;
cout<<"n Enter "<<rows<<"*"<<cols<<" Array Elements: n";
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
cout<<" ";
cin>>arr[i][j];
}
13.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 14
}
cout<<"n Two Dimensional Array is : n";
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
cout<<" "<<arr[i][j]<<" ";
}
cout<<"n";
}
return 0;
}
Output
Enter Rows for Array (Max 10): 3
Enter Columns for Array (Max 10): 3
Enter 3*3 Array Element :
1
2
3
4
5
6
7
8
9
Two Dimensional Array is :
1 2 3
4 5 6
7 8 9
12. C++ Program to Demonstrate Linear Search in Array
14.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 15
#include<iostream.h>
int main()
{
int arr[10], i, num, index;
cout<<"Enter 10 Numbers: ";
for(i=0; i<10; i++)
cin>>arr[i];
cout<<"nEnter a Number to Search: ";
cin>>num;
for(i=0; i<10; i++)
{
if(arr[i]==num)
{
index = i;
break;
}
}
cout<<"nFound at Index No."<<index;
cout<<endl;
return 0;
}
Output
Enter 10 Numbers:
1 2 3 4 5 6 7 8 9 10
Enter a Number to Search: 5
Found at Index No. 4
13. C++ program to demonstrate Binary Search in Array
#include<iostream.h>
int main()
{
15.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 16
int i, arr[10], num, first, last, middle;
cout<<"Enter 10 Elements (in ascending order): ";
for(i=0; i<10; i++)
cin>>arr[i];
cout<<"nEnter Element to be Search: ";
cin>>num;
first = 0;
last = 9;
middle = (first+last)/2;
while(first <= last)
{
if(arr[middle]<num)
first = middle+1;
else if(arr[middle]==num)
{
cout<<"nThe number, "<<num<<" found at Position "<<middle+1;
break;
}
else
last = middle-1;
middle = (first+last)/2;
}
if(first>last)
cout<<"nThe number, "<<num<<" is not found in given Array";
cout<<endl;
return 0;
}
Output
Enter 10 Elements (in ascenging Order):
1
2
16.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 17
3
4
5
6
7
8
9
10
Enter Element to be Search : 8
The number, 8 found at Position 8
14. C++ program to bubble sort in Array
#include<iostream.h>
int main()
{
int n, i, arr[50], j, temp;
cout<<"Enter the Size (max. 50): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"nSorting the Array using Bubble Sort Technique..n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
17.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 18
}
}
cout<<"nArray Sorted Successfully!n";
cout<<"nThe New Array is: n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output
Enter the Size(max. 50): 5
Enter 5 Numbers: 5 1 4 2 3
Sorting the Array using Bubble Sort Technique..
Array Sorted Successfully!
The new Array is:
1 2 3 4 5
15. C++ Program for Selection Sort in Array
#include<iostream.h>
int main()
{
int tot, arr[50], i, j, temp, small, chk, index;
cout<<"Enter the Size of Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
for(i=0; i<(tot-1); i++)
{
chk=0;
small = arr[i];
18.
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 19
for(j=(i+1); j<tot; j++)
{
if(small>arr[j])
{
small = arr[j];
chk++;
index = j;
}
}
if(chk!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
}
cout<<"nSorted Array is:n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output:
Enter the size of Array : 10
Enter 10 Array Elements :
10
1
9
2
8
3
प्रैक्टिकल कॉपी –क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
प्रैक्टिकल कॉपी – क
ं प्यूिर साइंस | झारखण्ड बोडड – कक्षा 12
YouTube Channel: @jharkhandeducation, @computerkaksha
P a g e | 21
भाग -२
Q. Write SQL Commands for (i) to (vii) on the basis of the table SPORTS
Table : SPORTS
Studnet _no Class Name Game1 Grade1 Game2 Grade2
10 7 Sammer Cricket B Swimming A
11 8 Sujit Tennis A Skating C
12 7 Kamal Swimming B Football B
13 7 Venna Tennis C Tennis A
14 9 Archana Basketball A Cricket A
15 10 Arpit Cricket A Athletics C
i. Display the names of the students who have grade ‘C’ in either Game1 or
Game2 or both.
ii. Display the number of students getting grade ‘A’ in cricket.
iii. Display the names of the students who have same game for both Game1
and Game2.
iv. Display the games taken up by the students, whose name start with ‘A’.
v. Add a new column named ‘Marks’
vi. Assign value 200 for marks for all those who are getting grade ‘B’ or
grade ‘A’ in both Game1 and Game2
vii. Arrange the whole table in the alphabetical order of name.
Query (Command)
i. SELECT name FROM Sports WHERE Grade1=’C” OR Grade2=’C’;
ii. SELECT count(*) FROM Sports WHERE(Grade1=’A’ AND
Game1=’Cricket’) OR (Grade=’A’ and Game2=’Cricket’);
iii. SELECT name FROM Sports Game1=Game2 WHERE Game1=Game2;
iv. SELECT Game1, Game2 FROM Sports WHERE name LIKE=’A%’;
v. ALTER TABLE Student ADD Marks float (6, 2);
vi. UPDATE Student SET Marks=200 WHERE Grade1<=”B” AND
Grade2<=”B”;SELECT FROM Sports ORDER BY Name;