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

C++ Lab_Manual_4th_Sem_Mech-xyz

The C++ Programming Lab Manual from Bahubali College of Engineering includes a series of programming exercises focused on various concepts such as classes, structs, console I/O operations, and pointers. Each exercise provides an algorithm, source code, and expected output to help students understand and implement C++ programming techniques. The manual covers topics like employee data management, scope resolution, and using arrays of pointers.

Uploaded by

skandabn8762
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)
7 views

C++ Lab_Manual_4th_Sem_Mech-xyz

The C++ Programming Lab Manual from Bahubali College of Engineering includes a series of programming exercises focused on various concepts such as classes, structs, console I/O operations, and pointers. Each exercise provides an algorithm, source code, and expected output to help students understand and implement C++ programming techniques. The manual covers topics like employee data management, scope resolution, and using arrays of pointers.

Uploaded by

skandabn8762
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/ 13

Bahubali College of Engineering

Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

C++ Programming Lab Manual

INDEX

Sl. No. Content Page No


1 Write a C++ program to display Names, Roll No., and grade of 3 students who 2
have appeared in the examination. Declare the class of name, roll no., and grade.
Create an array of class objects. Read and display the contents of the array.
2 Write a C++ program to declare struct. Initialize and display contents of member. 4
3 Write a C++ program to declare a class. Declare pointer to class. Initialize and 5
display the contents of the class members.
4 Given that EMPLOYEE class contains following members: data members: 6
Employee number, Employee name, Basic, DA, IT, Net Salary, and print data
members.
5 Write a C++ program to read the data of N employee and compute the net salary 8
of each employee (DA=52% of basic and IT=30% of gross salary).
6 Write a C++ to illustrate the concepts of console I/O operations. 9
7 Write a C++ program to use scope resolution operator. Display the various values 12
of the same variables declared at different scope levels.
8 Write a C++ program to create an array of pointers. Invoke functions using array 13
objects.

1
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

1. Write a C++ program to display Names, Roll No., and grade of 3 students who have appeared
in the examination. Declare the class of name, roll no., and grade. Create an array of class
objects. Read and display the contents of the array.

Algorithm:
1. Define the class consisting the members of name, rollno and grade of the student.
2. Read the student name, roll number and grade.
3. Print the student name, roll number and grade.

Source Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int k=0;
class stud
{
public: char name[12];
int rollno;
char grade[2];
};
class stud st[3];
while(k<3)
{ clrscr();
gotoxy(2,4);
cout<<”Name”;
gotoxy(17,4);
cin>>st[k].name;
gotoxy(2,5);
cout<<”Roll Number”;
gotoxy(17,5);
cin>>st[k].rollno;
cout<<”Grade”;
gotoxy(17,6);
cin>>st[k].grade;
st[k].grade[1]=’\0’;
puts(“press any key..”);
getch();
k++;
}
k=0;
clrscr();
cout<<”\n Name\tRoll No.\t Grade\n”;
while(k<3)
{
cout<<st[k].name<<”\t”<<st[k].rollno<<”\t”<<st[k].grade<<”\n”;
k++;
}
}

2
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

Output:
Name RollNo Grade
Balaji 50 A
Manoj 51 B
Sanjay 55 C

3
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

2. Write a C++ program to declare struct. Initialize and display contents of member
variables.

Algorithm:
1. Define the structure consisting of member’s name, roll number and marks of the student.
2. Read the student name, roll number and marks of the student.
3. Print the student name, roll number and marks of the student.

Source Code:
#include <iostream> using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
cout << "\nDisplaying Information," << endl;
cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0;
}

Output
Enter information,
Enter name: Bill
Enter roll number: 4
Enter marks: 55.6
Displaying Information,
Name: Bill
Roll: 4
Marks: 55.6

4
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display
the contents of the class members.

Algorithm:
1. Define a class with one class member.
2. Declare the pointer object to the class in the main function.
3. In main function, with the pointer object we can access the class member.

Source Code
class Simple
{
public:
int a=5;
};
int main()
{
simple obj;
simple* ptr; // Pointer of class type
ptr = &obj;
cout << obj.a;
cout << ptr->a; // Accessing member with pointer
}

Output:
5
5

5
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

4. Given that EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary, and print data members.

Source Code:
#include<iostream>
using namespace std;
class employee
{
int empno;
char empname[50];
int basic;
int da;
int it;
int gs;
int ns;
Public:
void getdata()
{
cout<<”enter the employee number”;
cin>>empno;
cout<<”enter the employee name”;
cin>>empname;
cout<<”enter the basic salary,da,it”;
cin>>basic>>da>>it;
gs=da+it;
ns=gs+basic;
}
void putdata()
{
cout<<”\nEmployee number”<<empno;
cout<<”\nEmployee name”<<empname;
cout<<”\nbasic\tda\tit\n”<<basic<<da<<it;
cout<<”\n gross salary\n net salary”<<gs<<ns;
}
};
void main()
{
employee e;
e.getdata();
e.putdata();
}

Output:
Enter the employee number 101
Enter the employee name Rakesh
Enter the basic salary, da, it
15000
500
600

6
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

Employee number101
Employee name Rakesh
Basic da ta 15000 500 600
Gross salary1100
Net salary 16100

7
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

5. Write a C++ program to read the data of N employee and compute the net salary of each
employee (DA=52% of basic and IT=30% of gross salary).

Source Code
#include<iostream>
using namespace std;
class employee
{
int empno;
char empname[50];
int basic;
int da;
int it;
int gs;
int ns;
public:
void getdata()
{
cout<<”enter the employee number”;
cin>>empno;
cout<<”enter the employee name”;
cin>>empname;
cout<<”enter the basic salary,da,it”;
cin>>basic>>da>>it;
gs=basic+da;
ns= basic+da-it
}
void putdata()
{
cout<<”\nEmployee number”<<empno;
cout<<”\nEmployee name”<<empname;
cout<<”\nbasic\tda\tit\n”<<basic<<da<<it;
cout<<”\n gross salary\n net salary”<<gs<<ns;
}
};
void main()
{
employee e[20];
int n,i;
cout<<”enter the number of employees”;
cin>>n;
for(i=0;i<n;i++)
{
cout<<”enter the employee <<i<< details<<”\n”;
e[i].getdata();
e[i].putdata();
}

8
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

6. Write a C++ to illustrate the concepts of console I/O operations

a) get()
#include<iostream>
using namespace std;
int main()
{
char c=cin.get();
cout<<c<<endl;
return 0;
}

Output
I
I

b) put()
#include<iostream>
using namespace std;
int main()
{
char c=cin.get();
cout.put(c); //Here it prints the value of variable c;
cout.put('c'); //Here it prints the character 'c';
return 0;
}

Output
I
Ic

C) getline(char *buffer,int size)


#include<iostream>
using namespace std;
int main()
{
cout<<"Enter name :";
char c[10];
cin.getline(c,10); //It takes 10 charcters as input;
cout<<c<<endl;
return 0;
}

Output
Enter name :Divyanshu
Divyanshu

D) write(char * buffer, int n)


#include<iostream>
using namespace std;
int main()

9
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

{
cout<<"Enter name : ";
char c[10];
cin.getline(c,10); //It takes 10 charcters as input;
cout.write(c,9); //It reads only 9 character from buffer c;
return 0;
}

Output
Enter name: Divyanshu
Divyanshu

E) cin
#include<iostream>
using namespace std;
int main()
{
int num;
char ch;
string str;
cout<<"Enter Number"<<endl;
cin>>num; //Inputs a variable;
cout<<"Enter Character"<<endl;
cin>>ch; //Inputs a character;
cout<<"Enter String"<<endl;
cin>>str; //Inputs a string;
return 0;
}

Output
Enter Number
07
Enter Character
h
Enter String
Deepak

F) cout
#include<iostream>
using namespace std;
int main()
{
int num=100;
char ch='X';
string str="Deepak";
cout<<"Number is "<<num<<endl; //Prints value of variable;
cout<<"Character is "<<ch<<endl; //Prints character;
cout<<"String is "<<str<<endl; //Prints string;
return 0;
}

10
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

Output
Number is 100
Character is X
String is Deepak

11
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

7. Write a C++ program to use scope resolution operator. Display the various values of the
same variables declared at different scope levels.

Source Code:
#include<iostream>
int n = 12; //global variable
int main()
{
int n = 13; //local variable
cout << ::n << endl; //print global variable:12
cout << n << endl; //print the local variable:13
}

Output:
12
13

12
Bahubali College of Engineering
Shravanabelagola

DEPARTMENT OF MECHANICAL ENGINEERING

8. Write a C++ program to create an array of pointers. Invoke functions using array objects

Source Code:
#include <iostream>
using namespace std;
const int MAX = 3;
int main (){
int var[MAX] = {10, 100, 200};
int *ptr[MAX];
for (int i = 0; i < MAX; i++){
ptr[i] = &var[i]; // assign the address of integer.
}
for (int i = 0; i < MAX; i++)
{
cout << "Value of var[" << i << "] = ";
cout << *ptr[i] << endl;
}
return 0;
}

Output:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy