Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

R20CSE21L3 C Programming Lab

Uploaded by

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

R20CSE21L3 C Programming Lab

Uploaded by

vishakha soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

S

HANDOUT
II Year I Semester

DEPARTMENT OF COMPUTER
SCIENCE AND ENGINEERING
ACADEMIC YEAR 2022-2023
SRI INDU COLLEGE OF ENGINEERING & TECHNOLOGY
(An Autonomous Institution under UGC, New Delhi)
(Permanently Affiliated to JNTUH, Approved by AICTE, New Delhi and Accredited by NBA, NAAC)
Sheriguda Village, Ibrahimpatnam Mandal, Ranga Reddy Dist. – 501 510

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

LAB HANDOUT- INDEX


S. No Contents
1 Vision, Mission, PEOs, PSOs,POs& Cos
2 Institution Academic Calendar
3 Department Academic Calendar
4 Lab Manual
SRI INDU COLLEGE OF ENGINEERING & TECHNOLOGY
(An Autonomous Institution under UGC, New Delhi)
(Permanently Affiliated to JNTUH, Approved by AICTE, New Delhi and Accredited by NBA, NAAC)
Sheriguda Village, Ibrahimpatnam Mandal, Ranga Reddy Dist. – 501 510
I
INSTITUTION VISION
To be a premier institution in engineering & technology and management with competence, values
and social consciousness.
INSTITUTION MISSION
IM1: Provide high quality academic programmes, training activities and research facilities.

IM2: Promote continuous industry-institute interaction for employability, entrepreneurship,


leadership and research aptitude among stakeholders.

IM3: Contribute to the economic and technological development of the region, state and
nation.
SRI INDU COLLEGE OF ENGINEERING & TECHNOLOGY
(An Autonomous Institution under UGC, New Delhi)
(Permanently Affiliated to JNTUH, Approved by AICTE, New Delhi and Accredited by NBA, NAAC)
Sheriguda Village, Ibrahimpatnam Mandal, Ranga Reddy Dist. – 501 510

VISION OF THE DEPARTMENT

To be a technologically adaptive centre for computing by grooming the students as top notch
professionals.

MISSION OF THE DEPARTMENT


DM1: To offer quality education in computing.

DM2: To provide an environment that enables overall development of all the stakeholders.

DM3: To impart training on emerging technologies like Data Analytics, Artificial Intelligence
and Internet Of Things.

DM4: To encourage participation of stakeholders in research and development


Program Educational Objectives(PEO’s)
Higher Studies : Graduates with an ability to apply knowledge of Basic Sciences
PEO1 and programming skills in their career and higher education.
Lifelong Learning: Graduates with an ability to adopt new technologies for
PE02 ever changing IT industry needs through Self-Study, Critical thinking and
Problem solving skills.
Professional Skills : Graduates will be ready to work in projects related to
PEO3 complex problems involving multidisciplinary projects with effective analytical
skills
Engineering citizenship: Graduates with an ability to communicate well and
PEO4 exhibit social,technical and ethical responsibility in process or product.

Program Specific Outcomes(PSO’s)

Software Development: To apply the knowledge of Software Engineering, Data


PSO1 Communication, Web Technology and Operating Systems for building
IOT and Cloud Computing applications.

Industrial Skills Ability: Design, develop and test software systems for world-
PSO2 wide network of computers to provide solutions to real world
problems.

Project Implementation: Analyze and recommend the appropriate IT


PSO3
infrastructure required for the implementation of a project.

Program Outcomes(PO’s)

Engineering Knowledge: Apply the knowledge of mathematics, science, engineering


PO1 fundamentals, and an engineering specialization to the solution of complex engineering
problems.
Problem Analysis: Identify, formulate, review research literature, and analyze complex
PO2 engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
Design / Development of Solutions: Design solutions for complex engineering problems and
PO3 design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
Conduct investigations of complex problems: Use research-based knowledge and
PO4 research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.

Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
PO5 engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.

The engineer and society: Apply reasoning informed by the contextual knowledge to assess
PO6 societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
Environment and sustainability: Understand the impact of the professional engineering
PO7 solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
PO8 norms of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member or leader
PO9 in diverse teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities with the
PO10 engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of the
PO11 engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.

Life-long learning: Recognize the need for, and have the preparation and ability to engage in
PO12 independent and life-long learning in the broadest context of technological change.
COURSE OUTCOMES (CO’s)
Academic Year: 2022-23

Class: II YEAR-I SEM.

Course Name: C++ Programming (R20CSE21L3)

At the end of the course, the student will be able to

Course Outcomes (COs)


Develop applications for a range of problems using object-
C21L3.1 oriented programming(Create)

Demonstrate the implementation of constructors,


C21L3.2 destructors and operatoroverloading. (Apply)

Apply virtual and pure virtual function & complex program situations(Apply)
C21L3.3
Apply fundamental algorithmic problems including type
C21L3.4 casting, inheritance, andpolymorphism. (Apply)

C21L3.5 Explain generic programming, templates, file handling. (Understand)

Handle exceptions in programming (Analyze)


C21L3.6

Mapping of Course Outcomes(CO’s) with PO’s:

PO
CO
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
3 2 3 3 1 1 - - - - 3 1
C21L3.1
3 3 3 3 - - - - - - - 1
C21L3.2
3 3 3 3 - - - - 1 - 1 1
C21L3.3
3 2 3 2 - 1 - - - - - 1
C21L3.4
C21L3.5 3 3 3 3 - - - - 1 - - 2
3 3 3 3 - - - - - - - 1
C21L3.6
3 2.6 3 2.6 - 1 - - 1 - 3 1.5

3: High 2. Medium 1. Low


Mapping of Course Outcomes(CO’s) with PSO’s:

Cos PSO1 PSO2 PSO3


3 3 -
C21L3.1
3 3 -
C21L3.2
3 3 2
C21L3.3
3 3 3
C21L3.4
C21L3.5 3 3 2
3 3 -
C21L3.6
3 3 3\
SRI INDU COLLEGE OF ENGINEERING AND TECHNOLOGY (AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DEPARTMENT Academic CALENDAR – 2022-2023 (SEMESTER-1)

DAYS
SUNDAY NOVEMBER ‘22
DECEMBER
MONDAY 1 FEBRUARY’23
‘22
SEPTEMBER‘2
TUESDAY 2 1
2
PRACTICAL
WEDNESDAY 3 1 2
EXAM
PRACTICAL
THURSDAY 1 OCTOBER ‘22 4 2 3
EXAM
Bathukamm
a PRACTICAL
FRIDAY 2 1 5 3 JANUARY’23 4
Celebration EXAM
s
Gandhi NEW YEAR/
SATURDAY 3 2 Jayanti/ 6 HOLIDAY 4 HOLIDAY 1 5 HOLIDAY
HOLIDAY
HOLIDAY
DASARA PRACTICAL
SUNDAY 4 HOLIDAY 3 7 5 2 6
HOLIDAYS EXAM
DASARA GURUNAN
PRACTICAL
MONDAY 5 4 HOLIDAYS 8 AK 6 3 7
EXAM
JAYANTHI
DASARA PRACTICAL
TUESDAY 6 5 9 7 4 8
HOLIDAYS EXAM
DASARA PRACTICAL
WEDNESDAY 7 6 10 8 5 9
HOLIDAYS EXAM
DASARA PRACTICAL
THURSDAY 8 7 11 9 6 10
HOLIDAYS EXAM
DASARA PRACTICAL
FRIDAY 9 8 12 10 7 11
HOLIDAYS EXAM
SATURDAY10 9 HOLIDAY 13 HOLIDAY 11 HOLIDAY 8 H OLIDAY 12 HOLIDAY

SUNDAY11 HOLIDAY 10 14 12 9 13 END EXAM

MONDAY12 11 15 13 10 14 END EXAM

TUESDAY13 12 16 14 11 15 END EXAM

WEDNESDAY14 13 17 15 12 16 END EXAM


THURSDAY15 14 18 16 13 BHOGI 17 END EXAM

FRIDAY16 15 19 17 14 SANKRANTHI 18 END EXAM


Telangana
SATURDAY17 vimochana 16 HOLIDAY 20 HOLIDAY 18 HOLIDAY 15 HOLIDAY 19 HOLIDAY
dinostavam
SUNDAY18 HOLIDAY 17 21 19 16 HOLIDAY 20 END EXAM

MONDAY19 18 22 20 17 21 END EXAM

TUESDAY20 19 23 21 18 22 END EXAM

WEDNESDAY21 20 24 22 19 23 END EXAM

THURSDAY22 21 25 23 20 24 END EXAM

FRIDAY23 22 26 24 21 25 END EXAM


CHRISTMAS/
SATURDAY24 23 HOLIDAY 27 HOLIDAY 25 22 HOLIDAY 26
HOLIDAY
BOXING DAY/
SUNDAY25 HOLIDAY 24 28 26 23 27
HOLIDAY
Commencemen
MONDAY26t of Classes (II25 29 27 24 28
Yr)
TUESDAY27 26 30 28 25 29

WEDNESDAY28 27 29 26 Republic Day


THURSDAY29 28 30 27
FRIDAY30 29 31 28
SATURDAY 30 29 HOLIDAY
SUNDAY 31 30
MONDAY 31

HOD/CSE
CALENDAR INCHARGE DEAN PRINCIPAL
SRI INDU COLLEGE OF ENGINEERING & TECHNOLOGY
(An Autonomous Institution under UGC, New Delhi)

B.Tech. - II Year – I Semester L T P C


0 0 2 1

(R20CSE21L3) C++ Programming Lab


Course Objectives
 Introduces object-oriented programming concepts using the C++ language.
 Introduces the principles of data abstraction, inheritance and polymorphism;
 Introduces the principles of virtual functions and polymorphism
 Introduces handling formatted I/O and unformatted I/O
 Introduces exception handling
Course Outcomes
 Ability to develop applications for a range of problems using object-oriented programming
Techniques
 Demonstrate the concepts of inheritance and polymorphism.
 Difference between function overloading and function overriding
 Explain exception handling in object-oriented programs.
 Use template classes and the STL library in C++.
 write object-oriented programs of moderate complexity in C++
 Analyze operators in C++.

LIST OF EXPERIMENTS

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.
SRI INDU COLLEGE OF ENGG & TECH
Prepared on
LAB PLAN Rev1:
(Regulation
:R20) Page: 1 of 4
Department of Computer Science and Engineering

(R20CSE21L3)
Sub. Code & Object Oriented Programming using C++ LAB
Title
Academic Year: Year/Sem./Sect
2022-23 ion II/I/A&B&C&D
1. DR.N.SADASHIVAM
(PROFESSOR),
2. A.SUDHEER ASST.PROF,
Faculty Name & 3. G.AKILA ASST.PROF,
Designation 4. K.KRISHNA ASST.PROF

Lab Plan
2022-23 II Year –I Semester CSE
No. of weeks
S No Topics

1. 1 Write a C++ Program to display Names, Roll No., and grades of 3 students 1
who have appearedin 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. 2 Write a C++ program to declare Struct. Initialize and display contents of member 1
variables.

3. 2 Write a C++ program to declare a class. Declare pointer to class. 1


Initialize and display thecontents of the class member.

4. 2 1

Given that an EMPLOYEE class contains following members: data


members: Employeenumber, Employee name, Basic, DA, IT, Net
Salary and print data members.

5. 1 Write a C++ program to read the data of N employee and compute Net salary 1
of each employee(DA=52% of Basic and Income Tax (IT) =30% of the gross
salary).

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

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

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

9. 2 Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, 1
A3)

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

11. Write a C++ program to use pointer for both base and derived classes and call 1
the memberfunction. Use Virtual keyword.
Object Oriented Programming using C++ Lab Manual
(Subject Code: R20CSE21L3)
Computer Science &Engineering

LabManual

C++Programming

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 thearray.
2. Write a C++ program to declare Struct. Initialize and display contents of membervariables.
3. Write a C++ program to declare a class. Declare pointer to class. Initialize and displaythe
contents of the classmember.
4. Given that an EMPLOYEE class contains following members: data members:Employee
number, Employee name, Basic, DA, IT, Net Salary and print datamembers.
5. Write a C++ program to read the data of N employee and compute Net salary ofeach
employee (DA=52% of Basic and Income Tax (IT) =30% of the grosssalary).
6. Write a C++ to illustrate the concepts of console I/Ooperations.
7. Write a C++ program to use scope resolution operator. Display the various values of thesame
variables declared at different scopelevels.
8. Write a C++ program to allocate memory using newoperator.
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 arrayobjects.
11. Write a C++ program to use pointer for both base and derived classes and call themember
function. Use Virtualkeyword
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:
charname[50];
int roll;
chargrade;
} s[3];

int main() {
cout <<"Enterinformation of students:" << endl;

// storinginformation
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;

// Displayinginformation
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;
}
Output:
Week 2:

Aim:Write a C++ program to declare Struct. Initialize and display contents of member variables.

Source Code:

#include <iostream> using


namespace std;

struct Person
{
charname[50];
int age;
float salary;
};

int main()
{
Personp1;

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;
}
Output:
Week 3:

Aim:Write a C++ program to declare a class. Declare pointer to class. Initialize and
display the contents of the class member.

Sourcecode:

#include <iostream>

using namespacestd;

classData
{
public:
inta;
void print()
{
cout <<"\n" <<"Value of 'a' is "<< a <<"\n";
}
};

int main()
{
Data d, *dp;
dp=&d; // pointer toobject

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

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

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

return 0;
}
Output:
Week 4:

Aim: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;

classemployee
{
int emp_number;
char
emp_name[20];f
loat emp_basic;
floatsal;
floatemp_da;
floatemp_net_sal;
floatemp_it;

public:
void get_details(int i);
void find_net_sal();
voidshow_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\nDetailsof : "<<emp_name;
cout<<"\n\nEmployeenumber: "<<emp_number;
cout<<"\nBasicsalary : "<<emp_basic;
cout<<"\nEmployeeDA : "<<emp_da;
cout<<"\nIncomeTax : "<<emp_it;
cout<<"\nNetSalary : "<<emp_net_sal;
}

intmain() {
employeeemp[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].show_emp_details();

getch();
return 0;
}

Output:
Week 5:
Aim: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];f
loat emp_basic;
floatsal;
floatemp_da;
float net_sal;
float emp_it;

public:
void get_details(int i);
void find_net_sal();
voidshow_emp_details();
};

void employee :: get_details(int i) {


cout<<"\nEnteremployee"<<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*********EmployeeDetails*********\n";
cout<<"\nDetailsof : "<<emp_name;
cout<<"\n\nEmployeenumber:
"<<emp_num;cout<<"\nBa
sicsalary : "<<emp_basic;
cout<<"\nEmployeeDA : "<<emp_da;
cout<<"\nIncomeTax : "<<emp_it;
cout<<"\nNetSalary : "<<net_sal;
}
int main() {
employeeemp[10];
int i,num;

cout<<"\nEnternumberofemployeedetails:";
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:
Week6:

Aim: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<<"\nEnteranycharactervalue:";
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:
#include <iostream>
#include<conio.h>

using namespace std;

int main() {
cout<<"\nEnteranyname:";
charc[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:
#include <iostream>
#include<conio.h>

using namespace std;

int main() {
int num; char
ch; stringstr;
cout<<"\nEnterNumber:";
cin>>num; //Inputs a variable;
cout<<"Enter Character: ";
cin>>ch; //Inputs a character;
cout<<"Enter String: "; cin>>str;
//Inputs astring;
cout<<endl<<"Youhaveentered:\nNumber:"<<num<<"\nCharacter:"
<<ch<<"\nString:"<<str<<endl;
return 0;

Output:
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:
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 =newdouble; // Request memory for thevariable

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


cout<<"\nValueofpvalue:"<<*pvalue<<endl;

delete pvalue; // free up thememory.

return 0;
}

Output:
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;
voidreaddata()
{
cout <<"\nEnter value of y= "; cin >>y;
}
};
class derive2 :publicderive1 // derived from classderive1
{

private:
int z;
public:
void indata() {
cout <<"\nEnter value of z= "; cin >>z;
}
void product() {
cout<<"\nProduct="<<x*y*z<<endl;
}
};

int main() {
derive2a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();

return 0;

}
Output:

Viva Questions:

1. What is Inheritance?
2.Types ofinheritance?
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;

classStudent
{
stringname; int
marks; public:
voidgetName()
{
getline( cin, name);
}
voidgetMarks()
{
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;
}
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
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;
}
Output:

You might also like