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

C++ Programming Lab Manual R18 JNTUH

The document is a lab manual for a C++ programming course. It provides: 1) An outline of the syllabus which includes 12 programming assignments ranging from basic programs to more advanced topics like inheritance and pointers. 2) A sample lesson plan showing one way to schedule and assess the assignments over 14 weeks. 3) Examples of the code and output for the first few assignments, along with sample viva questions, to demonstrate the expectations and guide students. 4) Additional resources like program outcomes and how assignments map to these, to situate the lab work within the overall course objectives.

Uploaded by

AEMN M24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views

C++ Programming Lab Manual R18 JNTUH

The document is a lab manual for a C++ programming course. It provides: 1) An outline of the syllabus which includes 12 programming assignments ranging from basic programs to more advanced topics like inheritance and pointers. 2) A sample lesson plan showing one way to schedule and assess the assignments over 14 weeks. 3) Examples of the code and output for the first few assignments, along with sample viva questions, to demonstrate the expectations and guide students. 4) Additional resources like program outcomes and how assignments map to these, to situate the lab work within the overall course objectives.

Uploaded by

AEMN M24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

www.btechsmartclass.

com

Lab Manual

C++ Programming
Computer Science & Engineering | Information Technology
(II- B. Tech. – I– Semester)

Regulation R18

Visit – www.btechsmartclass.com to get more like


 Study Materials
 Presentations
 Video Lectures and many more
www.btechsmartclass.com

1. Syllabus
CS309PC: C++ Programming LAB

B.Tech. II Year I Sem. LTPC


0 0 3 1.5
1. 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.
2. Write a C++ program to declare Struct. Initialize and display contents of member variables.
3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.
4. Given that an EMPLOYEE class contains following members: data members: 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 Net salary of each
employee (DA=52% of Basic and Income Tax (IT) =30% of the gross salary).
6. Write a C++ to illustrate the concepts of console I/O operations.
7. Write a C++ program to use scope resolution operator. Display the various values of the same
variables declared at different scope levels.
8. Write a C++ program to allocate memory using new operator.
9. Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
10. Write a C++ program to create an array of pointers. Invoke functions using array objects.
11. Write a C++ program to use pointer for both base and derived classes and call the member
function. Use Virtual keyword..
www.btechsmartclass.com

PROGRAM OUTCOMES (PO’s)

PO No. Program Outcomes (PO’s)

PO1 An ability to apply knowledge of computing, mathematics, science and engineering


fundamentals appropriate to the discipline.

PO2 An ability to analyze a problem, and identify and formulate the computing
requirements appropriate to its solution.

PO3 An ability to design, implement, and evaluate a computer-based system, process,


component, or program to meet desired needs with appropriate consideration for public
health and safety, cultural, societal and environmental considerations.

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.

PO7 Knowledge of contemporary issues.

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.

PO10 An ability to communicate effectively with a range of audiences.

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..

Write a C++ program to read the data of N


employee and compute Net salary of each
6 1 T1 Viva&Execution
employee (DA=52% of Basic and Income Tax
(IT) =30% of the gross salary).
Write a C++ to illustrate the concepts of
7 console I/O operations., 1 T1 Viva&Execution

Write a C++ program to use scope resolution


8 operator. Display the various values of the same 1 T1 Viva&Execution
variables declared at different scope levels.
Write a C++ program to allocate memory using
9 new operator. 1 T1 Viva&Execution

Write a C++ program to create multilevel


10 1 T1 Viva&Execution
inheritance. (Hint: Classes A1, A2, A3)
Write a C++ program to create an array of
11 1 T1 Viva&Execution
pointers. Invoke functions using array objects
Write a C++ program to use pointer for both
12 base and derived classes and call the member 1 T1 Viva&Execution
function. Use Virtual keyword.
13 LEAD Experments 1 T1 Viva&Execution
Total no of HOURS required to complete
14
syllabus
www.btechsmartclass.com

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>

using namespace std;

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;

cout << "Enter name: ";


cin >> s[i].name;

cout << "Enter Grade: ";


cin >> s[i].grade;

cout << endl;


}

cout << "Displaying Information: " << 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:

1.How do you declare a class in C++.


2.What is the Difference between class and structure.
www.btechsmartclass.com

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;

cout << "Enter Full name: ";


cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;

cout << "\nDisplaying Information." << endl;


cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;

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>

using namespace std;

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

int Data::*ptr=&Data::a; // pointer to data member 'a'

d.*ptr=10;
d.print();

dp->*ptr=20;
dp->print();

return 0;
}
www.btechsmartclass.com

Output:

Viva Questions:

1. What are pointers?


2. How do you declare a pointer in c++?
3. What is the use of pointers in c++?
www.btechsmartclass.com

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>

using namespace std;

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();
};

void employee :: get_details(int i) {


cout<<"\nEnter employee " << i+1 << " number: ";
cin>>emp_number;
cout<<"\nEnter employee " << i+1 << " name: ";
cin>>emp_name;
cout<<"\nEnter employee " << i+1 << " basic: ";
cin>>emp_basic;
}

void employee :: show_emp_details() {


cout<<"\n\n\nDetails of : "<<emp_name;
cout<<"\n\nEmployee number: "<<emp_number;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<emp_net_sal;
}

int main() {
employee emp[10];
int i,num;

cout<<"\nEnter number of employee details: ";


www.btechsmartclass.com

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>

using namespace std;

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();
};

void employee :: get_details(int i) {


cout<<"\nEnter employee "<< i+1 <<" number: ";
cin>>emp_num;
cout<<"\nEnter employee "<< i+1 <<" name: ";
cin>>emp_name;
cout<<"\nEnter employee "<< i+1 <<" basic: ";
cin>>emp_basic;
}

void employee :: find_net_sal() {


emp_da=0.52*emp_basic;
emp_it=0.30*(emp_basic+emp_da);
net_sal=(emp_basic+emp_da)-emp_it;
}

void employee :: show_emp_details() {


cout<<"\n\n\n********* Employee Details *********\n";
cout<<"\nDetails of : "<<emp_name;
cout<<"\n\nEmployee number: "<<emp_num;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<net_sal;
}
www.btechsmartclass.com

int main() {
employee emp[10];
int i,num;

cout<<"\nEnter number of employee details: ";


cin>>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:

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

Source code:

#include <iostream>
#include<conio.h>

using namespace std;

int main() {
char c;

cout<<"\nEnter any character value: ";


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:
www.btechsmartclass.com

#include <iostream>
#include<conio.h>

using namespace std;

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>

using namespace std;

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:

1. Various types of console I/O operations form.


2. Explain Insertion and Extraction operator
www.btechsmartclass.com

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>

using namespace std;

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:

Write a C++ program to allocate memory using new operator.

Source code:

#include <iostream>
#include<conio.h>

using namespace std;

int main() {

double* pvalue = NULL; // Pointer initialized with null


pvalue = new double; // Request memory for the variable

*pvalue = 29494.99; // Store value at allocated address


cout << "\nValue of pvalue : " << *pvalue << endl;

delete pvalue; // free up the memory.

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>

using namespace std;

class base //single base class


{
public:
int x;
void getdata() {
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata() {
cout << "\nEnter value of z= "; cin >> z;
}
void product() {
cout << "\nProduct= " << x * y * z <<endl;
}
};

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>

using namespace std;

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();
}

for( int i=0; i<5; i++ )


{
cout << "Student " << i + 1 << endl;
st[i]->displayInfo();
}
return 0;
}
www.btechsmartclass.com

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:

1. What is a Pointer to an Array?


www.btechsmartclass.com

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 Bomb : public Weapon {


public:
void features() {
this->Weapon::features();
cout << "Loading bomb features.\n";
}
};

class Gun : public Weapon {


public:
void features() {
this->Weapon::features();
cout << "Loading gun 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:

1.What is virtual function?


2.Why virtual function is used?

You might also like