0% found this document useful (0 votes)
29 views25 pages

Vivek C++

The document contains a series of C++ practical assignments completed by Vivek Negi, detailing various programming concepts such as default arguments, function overloading, scope resolution operator, reference variables, inline functions, memory management with new and delete operators, and class implementation for student and employee data management. Each practical includes a problem statement, objective, explanation, source code, and output results. The practicals aim to enhance understanding of C++ programming techniques and object-oriented principles.
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)
29 views25 pages

Vivek C++

The document contains a series of C++ practical assignments completed by Vivek Negi, detailing various programming concepts such as default arguments, function overloading, scope resolution operator, reference variables, inline functions, memory management with new and delete operators, and class implementation for student and employee data management. Each practical includes a problem statement, objective, explanation, source code, and output results. The practicals aim to enhance understanding of C++ programming techniques and object-oriented principles.
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/ 25

Name: Vivek negi

Roll no.:- 74
Course: BCA2 (B1)
PRACTICAL NO. : -01

PROBLEM STATEMENT: Write a C++ Program to implement Default


argument.

OBJECTIVE: To understand the implementation of default argument in C++.

EXPLANATION: A default argument is a value in the function declaration


automatically assigned by the compiler if the calling function does not pass any
value to that argument.

SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
float amount;
float value(float p,int n,float r=0.15);
void printline(char ch='*',int
len=40); printline();
amount=value(5000.00,5,0.30);
cout<<"\n Final value = "<<amount;
amount= value(10000.00,5,0.30);
cout<<"\n Final value = "<<amount<<"\n";
printline('=');
return 0;
}
float value(float p,int n,float r)
{
int year=1;
float sum=p;
while(year<=n){
sum=sum*(1+r);
year=year+1;
}
return (sum);
}
void printline(char ch,int len){
for(int i=1;i<=len;i++){
printf("%c",ch);
}
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:

25
50
80
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)
PRACTICAL NO. : -02

Write a C++ Program to implement Function Overloading.

OBJECTIVE: To understand the implementation of Function Overloading in C+


+.

EXPLANATION: C++ lets you specify more than one function of the same name
in the same scope. These functions are called overloaded functions, or overloads.
Overloaded functions enable you to supply different semantics for a function,
depending on the types and number of its arguments.

SOURCE CODE:

#include<iostream>
using namespace std;
int area(int);
double area(double);
int area(int,int);
int main()
{
cout<<"Area of square is: "<<area(4)<<endl;
cout<<"Area of circle is: "<<area(3.5)<<endl;
cout<<"Area of rectangle is: "<<area(23,12)<<endl;
return 0;
}
int area(int a)
{
return(a*a);
}
double area(double r)
{
return((22/7)*r*r);
}
int area(int l,int b)
{
return(l*b);
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:

Area of square is: 16


Area of circle is: 36.75
Area of rectangle is:
276
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)
PRACTICAL NO. : -03

PROBLEM STATEMENT: Write a C++ Program to implement Scope


Resolution operator.

OBJECTIVE: To understand the implementation of scope resolution operator in


C++.

EXPLANATION: The :: (scope resolution) operator is used to qualify hidden


names so that you can still use them. You can use the unary scope operator if a
namespace scope or global scope name is hidden by an explicit declaration of the
same name in a block or class.

SOURCE CODE:

#include<iostream>
using namespace std;
int k=10;
int main()
{
int k=20;
{
int m=k;
int k=30;
cout<<"we are in inner
block."<<endl; cout<<"k=
"<<k<<endl;
cout<<"m= "<<m<<endl;
cout<<"::k= "<<::k<<endl;
}
cout<<"we are in outer
block."<<endl; cout<<"k=
"<<k<<endl;
cout<<"::k ="<<::k<<endl;
return 0;
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:

we are in inner block.


k= 30
m= 20
::k= 10
we are in outer block.
k= 20
::k =10
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)
PRACTICAL NO. : -04

PROBLEM STATEMENT: Write a C++ Program to implement reference


variable.

OBJECTIVE: To understand the implementation of a reference variable in C+


+.

EXPLANATION: A reference variable is one that refers to the address of


another variable. It represents the name of another variable, location, or value.
Once you initialize the variable references, that variable will be referred to using
the variable name or reference name.

SOURCE CODE:

#include<iostream>
using namespace std;
int main()
{
int i=10;
int *p=&i;
int **pt=&p;
int ***ptr=&pt;
cout<<"i= "<<i<<endl;
cout<<"*p= "<<p<<endl;
cout<<"*pt= "<<pt<<endl;
cout<<"*ptr= "<<ptr<<endl;
int a=5;
int &S=a;
int &S0=S;
int &S1=S0;
cout<<"a= "<<a<<endl;
cout<<"S= "<<S<<endl;
cout<<"S0= "<<S0<<endl;
cout<<"S1= "<<S1<<endl;
return 0;
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:

i= 10
*p= 0x70fdec
*pt= 0x70fde0
*ptr= 0x70fdd8
a= 5
S= 5
S0= 5
S1= 5
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)
PRACTICAL NO. : -05

PROBLEM STATEMENT: Write a C++ Program to implement Inline Function.

OBJECTIVE: To understand the implementation of inline function in C++.

EXPLANATION: Inline function in C++ is an enhancement feature that


improves the execution time and speed of the program. The main advantage of
inline functions is that you can use them with C++ classes as well.

SOURCE CODE:

#include<iostream>
using namespace std;
inline float mul(float x,float y)
{
return(x*y);
}
inline double div(double p,double q)
{
return(p/q);
}
int main()
{
float a=12.345;
float b=9.82;
cout<<"Product of a and b is:
"<<mul(a,b)<<endl; cout<<"Division of a and b
is: "<<div(a,b)<<endl; return 0;
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:

Product of a and b is: 121.228


Division of a and b is: 1.25713
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

PRACTICAL NO. : -06

PROBLEM STATEMENT: Write a C++ Program to implement new and delete


operator.

OBJECTIVE: To understand the implementation of new and delete operator in


C++.

EXPLANATION: The new operator requests for the memory allocation in heap.
If the sufficient memory is available, it initializes the memory to the pointer
variable and returns its address. The delete operator is used to deallocate the
memory. User has privilege to deallocate the created pointer variable by this
delete operator.

SOURCE CODE:

#include <iostream>
using namespace std;
int main ()
{
int *ptr1 = NULL;
ptr1 = new int;
float *ptr2 = new float(299.121);
int *ptr3 = new int[28];
*ptr1 = 28;
cout << "Value of pointer variable 1 : " << *ptr1 <<
endl; cout << "Value of pointer variable 2 : " << *ptr2
<< endl; if (!ptr3)
cout << "Allocation of memory failed\n";
else {
for (int i = 10; i < 15; i++)
ptr3[i] = i+1;
cout << "Value of store in block of memory:
"; for (int i = 10; i < 15; i++)
cout << ptr3[i] << " ";
}
delete ptr1;
delete ptr2;
delete[] ptr3;
return 0;
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:

Value of pointer variable 1 : 28


Value of pointer variable 2 : 299.121
Value of store in block of memory: 11 12 13 14 15
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

PRACTICAL NO. : -07

PROBLEM STATEMENT: Write a C++ Program to store Roll no., Name


and marks in 5 subjects for a student for a student in a class. Compute total
marks and percentage and display the details of the student.

OBJECTIVE: To understand the implementation of Class and Objects in C++.

EXPLANATION: In C++, a class is a user-defined data type that contains data


members and member functions. An object is an instance of a class. An object is
a variable of the class type, and it can be created using the new operator.

SOURCE CODE:

#include<iostream>
using namespace std;
class Student
{
private:
int rollno;
char Name[20];
int Marks[5],i,total_marks;
float percentage;
public:
void getdata()
{
cout<<"Enter roll no.:
"<<endl; cin>>rollno;
cout<<"Enter Name: "<<endl;
char ch=cin.get();
cin.getline(Name,20);
cout<<"Enter the Marks of all subjects: "<<endl;
for(i=0;i<5;i++)
{
cin>>Marks[i];
}
}
void compute()
{
for(i=0;i<5;i++)
{
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)
total_marks=total_marks+Marks[i];
}
percentage=(total_marks*100)/500;
}
void display()
{
cout<<"Student's Roll no.= "<<rollno<<endl;
cout<<"Student's Name= "<<Name<<endl;
cout<<"Total Marks of Student is= "<<total_marks<<endl;
cout<<"Percentage of Student is= "<<percentage<<endl;
}
}S;
int main()
{
S.getdata();
S.compute();
S.display();
return 0;
}

OUTPUT:

Enter roll no.:


11
Enter Name:
Anjali Gupta
Enter the Marks of all subjects:
89
91
94
90
86
Student's Roll no.= 11
Student's Name= Anjali
Gupta Total Marks of Student
is= 450 Percentage of Student
is= 90
Name: Vivek Negi
Roll no :- 74
Course: BCA2 (B1)

PRACTICAL NO. : -08

PROBLEM STATEMENT: Write a C++ Program to store Employee I’d,


Name, Designation and Basic salary of Employee in a class. Bonus will be 20%
of Basic salary. Compute Net salary of an Employee and display all the details
of 3 Employees.

OBJECTIVE: To understand the implementation of Class and Objects.

EXPLANATION: In C++, an object is an instance of a class. A class is a user-


defined data type that binds data and the functions that operate on the data
together in a single unit. Like other user-defined data types, it also needs to be
defined before using its objects in the program. A class definition specifies a
new data type that can be treated as a built-in data type.

SOURCE CODE:

#include<iostream>
using namespace std;
class Employee
{
private:
int id;
char name[20];
char designation[20];
int salary,bonus;
float net_salary;
public:
void getdata()
{
cout<<"Enter I'd:
"<<endl; cin>>id;
cout<<"Enter Name: "<<endl;
char ch=cin.get();
cin.getline(name,20);
cout<<"Enter Designation: "<<endl;
cin>>designation;

cout<<"Enter
Basic salary: "<<endl; cin>>salary;
Name: Vivek Negi
Roll no :- 74
Course: BCA2 (B1)
}
void compute()
{
bonus=(20*salary)/100;
net_salary=salary+bonus;
}
void display()
{
cout<<"Employee's I'd is: "<<id<<endl;
cout<<"Employee's Name is: "<<name<<endl;
cout<<"Designation of an Employee is:
"<<designation<<endl;
cout<<"Net Salary of an Employee is:
"<<net_salary<<endl;
}
}E[3];
int main()
{
int i;
for(i=0;i<3;i++)
{
cout<<"Enter details of an Employee "<<i+1<<endl;
E[i].getdata();
E[i].compute();
}
for(i=0;i<3;i++)
{
cout<<"Final details of an Employee "<<i+1<<endl;
E[i].display();

cout<<" "<<endl
;
}
return 0;
}
Name: Vivek Negi
Roll no :- 74
Course: BCA2 (B1)

OUTPUT:

Enter details of an Employee


1 Enter I'd:
101
Enter Name:
Sunil Singh
Enter Designation:
Manager
Enter Basic salary:
40000
Enter details of an Employee
2 Enter I'd:
102
Enter Name:
Atul Nautiyal
Enter
Designation:
Intern
Enter Basic salary:
20000
Enter details of an Employee
3 Enter I'd:
103
Enter Name:
Adhya Sharma
Enter
Designation:
Intern
Enter Basic salary:
20000
Final details of an Employee 1
Employee's I'd is: 101
Employee's Name is: Sunil
Singh
Designation of an Employee is:
Manager Net Salary of an Employee is:
48000

Final details of an Employee 2


Employee's I'd is: 102
Employee's Name is: Atul Nautiyal
Name: Vivek Negi
Roll no :- 74
Course: BCA2 (B1)

Designation of an Employee is: Intern


Net Salary of an Employee is: 24000
_
Final details of an Employee 3
Employee's I'd is: 103
Employee's Name is: Adhya Sharma
Designation of an Employee is: Intern
Net Salary of an Employee is: 24000

OUTPUT:

****************************************
Final value = 18564.6
Final value = 37129.3
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

PRACTICAL NO. : -09

PROBLEM STATEMENT: Write a C++ Program to elaborate passing objects


to functions.

OBJECTIVE: To understand the implementation of objects to functions in


class.

EXPLANATION: In C++ programming, we can pass objects to a function in a


similar manner as passing regular arguments. In C++ programming, we can
pass objects to a function in a similar manner as passing regular arguments.

SOURCE CODE:

#include<iostream>
using namespace std;
class Sample
{
int a;
public:
void getdata(int x)
{
a=x;
}
void sum(Sample s1,Sample s2)
{
a=s1.a+s2.a;
}
void display()
{
cout<<"Value of a: "<<a<<endl;
}
};
int main()
{
Sample s1,s2,s3;
s1.getdata(100);
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

s2.getdata(200);
s3.sum(s1,s2);
s1.display();
s2.display();
s3.display();
return 0;
}

OUTPUT:
Value of a:
100 Value of
a: 200 Value
of a: 300
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

PRACTICAL NO. : -10

PROBLEM STATEMENT: Write a C++ Program to implement the following:


A. Static data member
B. Static member function

OBJECTIVE: To understand the implementation of static data member and


static member function.

EXPLANATION: A static data member in C++ is a data member defined in a


class that is not instantiated with each object created of the class. Static
member functions in C++ are the functions that can access only the static data
members.

PART (A):-

SOURCE CODE:

#include<iostream>
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount(void)
{
cout<<"Count: "<<endl;
cout<<count<<endl;
}
};
int item::count;
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"After reading data"<<endl;
a.getcount();
b.getcount();
c.getcount();
return 0;
}

OUTPUT:
Count:
0
Count:
0
Count:
0
After reading data
Count:
3
Count
:
3
Count
:
3
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

PART (B):-

SOURCE CODE:

#include<iostream>
using namespace std;
class test
{
int code;
static int count;
public:
void getdata(void)
{
code=++count;
}
void showcode(void)
{
cout<<"Object number: "<<code<<endl;
}
static void showcount(void)
{
cout<<"Count: "<<count<<endl;
}
};int test::count;
int main()
{
test t1,t2;
t1.getdata();
t2.getdata();
test::showcount();
test t3;
t3.getdata();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

OUTPUT:
Count: 2
Count: 3
Object number: 1
Object number: 2
Object number: 3
Name: Vivek negi
Roll no.:- 74
Course: BCA2 (B1)

You might also like