C++ Programming Lab Manual R18 JNTUH
C++ Programming Lab Manual R18 JNTUH
com
Lab Manual
C++ Programming
Computer Science & Engineering | Information Technology
(II- B. Tech. – I– Semester)
Regulation R18
1. Syllabus
CS309PC: C++ Programming LAB
PO2 An ability to analyze a problem, and identify and formulate the computing
requirements appropriate to its solution.
PO4 An ability to design and conduct experiments, as well as to analyze and interpret data.
PO5 An ability to use current techniques, skills, and modern tools necessary for computing
practice.
PO6 An ability to analyze the local and global impact of computing on individuals, organizations,
and society.
PO8 An understanding of professional, ethical, legal, security and social issues and
responsibilities.
PO9 An ability to function effectively individually and on teams, including diverse and
multidisciplinary, to accomplish a common goal.
PO11 An understanding of engineering and management principles and apply these to one’s own
work, as a member and leader in a team, to manage projects.
PO12 Recognition of the need for and an ability to engage in continuing professional
development.
www.btechsmartclass.com
3.Lesson/Course Plan
No. of
Week Text Mode of
Name of the Program Hours
No. Books Assessment
required
1 Basic Programs 2 1 Viva&Execution
Write a C++ Program to display Names, Roll
No., and grades of 3 students who have
appeared in the examination. Declare the class
2 of name, Roll No. and grade. Create an array 1 T1 Viva&Execution
of class objects. Read and display the contents
of the array.
Write a C++ program to declare Struct.
3 Initialize and display contents of member 1 T1 Viva&Execution
variables
Write a C++ program to declare a class. Declare
4 pointer to class. Initialize and display the 1 T1 Viva&Execution
contents of the class member.
Given that an EMPLOYEE class contains
following members: data members: Employee
5 number, Employee name, Basic, DA, IT, Net 1 T1 Viva&Execution
Salary and print data members..
PROGRAMS
Week 1.
Aim: Write a C++ Program to display Names, Roll No., and grades 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.
Source Code:
#include <iostream>
class student {
public:
char name[50];
int roll;
char grade;
} s[3];
int main() {
cout << "Enter information of students: " << endl;
// storing information
for(int i = 0; i < 3; ++i) {
s[i].roll = i+1;
cout << "For roll number" << s[i].roll << "," << endl;
// Displaying information
for(int i = 0; i < 3; ++i) {
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Grade: " << s[i].grade << endl;
}
return 0;
}
www.btechsmartclass.com
Output:
Viva Questions:
Week 2:
Write a C++ program to declare Struct. Initialize and display contents of member variables.
Source Code:
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
return 0;
}
www.btechsmartclass.com
Output:
Viva Questions:
1. What is a structure?
2. What are structure Variables?
3. What is the difference between structure variable and member variable?
www.btechsmartclass.com
Week 3:
Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.
Source code:
#include <iostream>
class Data
{
public:
int a;
void print()
{
cout << "\n" << "Value of 'a' is "<< a << "\n";
}
};
int main()
{
Data d, *dp;
dp = &d; // pointer to object
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
return 0;
}
www.btechsmartclass.com
Output:
Viva Questions:
Week 4:
Given that an EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary and print data members.
Source code:
#include <iostream>
#include<conio.h>
class employee
{
int emp_number;
char emp_name[20];
float emp_basic;
float sal;
float emp_da;
float emp_net_sal;
float emp_it;
public:
void get_details(int i);
void find_net_sal();
void show_emp_details();
};
int main() {
employee emp[10];
int i,num;
cin>>num;
for(i=0;i<num;i++)
emp[i].get_details(i);
for(i=0;i<num;i++)
emp[i].show_emp_details();
getch();
return 0;
}
Output:
Viva Questions:
1. What is a class?
2. What are data members?
www.btechsmartclass.com
Week 5:
Write a C++ program to read the data of N employee and compute Net salary of each employee
(DA=52% of Basic and Income Tax (IT) =30% of the gross salary).
Source code:
#include <iostream>
#include<conio.h>
class employee {
int emp_num;
char emp_name[20];
float emp_basic;
float sal;
float emp_da;
float net_sal;
float emp_it;
public:
void get_details(int i);
void find_net_sal();
void show_emp_details();
};
int main() {
employee emp[10];
int i,num;
for(i=0;i<num;i++)
emp[i].get_details(i);
for(i=0;i<num;i++)
emp[i].find_net_sal();
for(i=0;i<num;i++)
emp[i].show_emp_details();
getch();
return 0;
}
Output:
Viva Questions:
1. What is a class?
2. What are data members?
3. How you will calculate net salary?
www.btechsmartclass.com
Week6:
Source code:
#include <iostream>
#include<conio.h>
int main() {
char c;
return 0;
}
Output:
www.btechsmartclass.com
#include <iostream>
#include<conio.h>
int main() {
cout<<"\nEnter any 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;
cout<<"\n";
return 0;
}
Output:
www.btechsmartclass.com
#include <iostream>
#include<conio.h>
int main() {
int num;
char ch;
string str;
cout<<"\nEnter Number: ";
cin>>num; //Inputs a variable;
cout<<"Enter Character: ";
cin>>ch; //Inputs a character;
cout<<"Enter String: ";
cin>>str; //Inputs a string;
cout<<endl <<"You have entered:\nNumber: "<<num<<"\nCharacter: "
<<ch<<"\nString: "<<str<<endl;
return 0;
Output:
Viva Questions:
Week 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>
#include<conio.h>
int x;
int main() {
int x = 10; // Local x
cout << "\nValue of global x is " << ::x;
cout << "\nValue of local x is " << x <<endl;
return 0;
Output:
Viva Questions:
1.What is importance of scope resolution operator?
2.Define scope of the variables?
www.btechsmartclass.com
Week 8:
Source code:
#include <iostream>
#include<conio.h>
int main() {
return 0;
}
Output:
Viva Questions:
1.What is dynamic memory allocation?
2.Explain new and delete operators.
www.btechsmartclass.com
Week 9:
Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
Source code:
#include <iostream>
#include<conio.h>
int main() {
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
www.btechsmartclass.com
Output:
Viva Questions:
1.What is Inheritance?
2.Types of inheritance?
www.btechsmartclass.com
Week 10:
Write a C++ program to create an array of pointers. Invoke functions using array objects.
Source code:
#include <iostream>
#include <string>
class Student
{
string name;
int marks;
public:
void getName()
{
getline( cin, name );
}
void getMarks()
{
cin >> marks;
}
void displayInfo()
{
cout << "Name : " << name << endl;
cout << "Marks : " << marks << endl;
}
};
int main()
{
Student st[5].*ptr;
ptr=&st;
for( int i=0; i<5; i++ )
{
cout << "Student " << i + 1 << endl;
cout << "Enter name" << endl;
st[i]->getName();
cout << "Enter marks" << endl;
st[i]->getMarks();
}
Output:
Output
Student 1
Enter name
Jack
Enter marks
54
Student 2
Enter name
Marx
Enter marks
45
Student 3
Enter name
Julie
Enter marks
47
Student 4
Enter name
Peter
Enter marks
23
Student 5
Enter name
Donald
Enter marks
87
Student 1
Name : Jack
Marks : 54
Student 2
Name : Marx
Marks : 45
Student 3
Name : Julie
Marks : 47
Student 4
Name : Peter
Marks : 23
Student 5
Name : Donald
Marks : 87
Viva Questions:
Week 11:
Write a C++ program to use pointer for both base and derived classes and call the member
function. Use Virtual keyword.
Source code:
#include <iostream>
using namespace std;
class Weapon {
public:
virtual void features() { cout << "Loading weapon features.\n"; }
};
class Loader {
public:
void loadFeatures(Weapon *weapon) {
weapon->features();
}
};
int main() {
Loader *l = new Loader;
Weapon *w;
Bomb b;
Gun g;
w = &b;
l->loadFeatures(w);
w = &g;
l->loadFeatures(w);
return 0;
}
www.btechsmartclass.com
Output:
Viva Questions: