0% found this document useful (0 votes)
9 views11 pages

notes

The document explains polymorphism in C++, defining it as the ability of a message to be displayed in multiple forms, with examples of compile-time and runtime polymorphism. It also covers key concepts in object-oriented programming such as encapsulation, data abstraction, and inheritance, and provides several C++ programming examples including a student class, palindrome check, largest number finder, and grade calculation using switch statements. Additionally, it discusses the syntax and functionality of switch and if-else statements.

Uploaded by

Surabhi Gharat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
9 views11 pages

notes

The document explains polymorphism in C++, defining it as the ability of a message to be displayed in multiple forms, with examples of compile-time and runtime polymorphism. It also covers key concepts in object-oriented programming such as encapsulation, data abstraction, and inheritance, and provides several C++ programming examples including a student class, palindrome check, largest number finder, and grade calculation using switch statements. Additionally, it discusses the syntax and functionality of switch and if-else statements.

Uploaded by

Surabhi Gharat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

1.

Explain polymorphism in brief


Ans C++ Polymorphism
The word “polymorphism” means having many forms. In simple words, we
can define polymorphism as the ability of a message to be displayed in more
than one form. A real-life example of polymorphism is a person who at the
same time can have different characteristics. A man at the same time is a
father, a husband, and an employee. So the same person exhibits different
behavior in different situations. This is called polymorphism. Polymorphism is
considered one of the important features of Object-Oriented Programming.

Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism

1.Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
A.Function Overloading
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is
known as Function Overloading. Functions can be overloaded by changing
the number of arguments or/and changing the type of arguments. In simple
terms, it is a feature of object-oriented programming providing many
functions that have the same name but distinct parameters when numerous
tasks are listed under one function name. There are certain Rules of Function
Overloading that should be followed while overloading a function.

Operator Overloading
C++ has the ability to provide the operators with a special meaning for a
data type, this ability is known as operator overloading. For example, we can
make use of the addition operator (+) for string class to concatenate two
strings. We know that the task of this operator is to add two operands. So a
single operator ‘+’, when placed between integer operands, adds them and
when placed between string operands, concatenates them.
Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding
and dynamic polymorphism are other names for runtime polymorphism. The
function call is resolved at runtime in runtime polymorphism. In contrast,
with compile time polymorphism, the compiler determines which function
call to bind to the object after deducing it at runtime.

A. Function Overriding
Function Overriding occurs when a derived class has a definition for one of
the member functions of the base class. That base function is said to be
overridden.

2.Define the following terms and explain them


a) Encapsulation
b) Data abstraction
c) Polymorphism
d) Inheritance
Ans Inheritance: When one class acquires all the properties and behaviors of
a parent object, it is known as inheritance. It provides code reusability.
Polymorphism: Polymorphism means having many forms, It is the ability of
an object to take on many forms.
Abstraction: It is the property by virtue of which only the essential details are
displayed to the user, the non-essential details are hidden from the end
users.
Encapsulation: It is defined as the wrapping up of data under a single unit. It
is the mechanism that binds together code and the data it manipulates.
3.Write a program to declare a class 'student' having data members
has name and percentage write a constructor to initialise this data
members accept and display data for one student.
Ans #include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char stud_name[20];
public:
void Accept();
void Display();
};
void student::Accept()
{
cout<<"\n Enter student‟s name and roll no\n";
cin>>stud_name>>roll_no;
}
void student::Display()
{
cout<<stud_name<<”\t”<<roll_no<<”\n”;
}
void main()
{
student S[5];
inti;
clrscr();
for(i=0;i<5;i++)
{
S[i].Accept();
}
cout<<”Student details \n Student‟s Name \t Roll No\n”
for(i=0;i<5;i++)
{
S[i].Display();
}
getch();
}

4.What is switch statement?Also write syntax for switch statement.


AnsThe switch statement is the control statement that allows any value to
change the control of the execution. The dispatch of execution to different
parts of the code is accessible using the C++ switch.

The expression in the switch statement of C++ is valid only if it results in a


constant value. The duplicate case values are not allowed in a switch
statement. The nesting of switch statements is allowed in C++. There are
options to switch inside the other statement.

There are two forms of switch statements. The switch statement in C++ is
unstructured as compared to the structured switches in modern languages
like Pascal. In a structured switch statement, it takes one branch, the
unstructured switch functions as the go-to type. The main keywords in switch
statements are the case, inspect, select, etc.

The switch statement in C++ improves the clarity in C++ programming and
reduces the bulkiness of repetitive coding. It ensures easy compiler
optimization and quick execution
C++ Switch Statement Syntax
The switch statement that compares the expression’s value with every single
case, for example, cases x and y is mentioned below.

Switch(expression) {
Case x:
// code block
break;
case y:
// code block
break;
default;
// code block
}

5. Write a program to find whether the string is palindrome.


Ans #include<iostream>
using namespace std;
int main()
{
int i,j,len,flag=1;
char a[20];
cout<<"Enter a string:";
cin>>a;
for(len=0;a[len]!='\0';++len);
for(i=0,j=len-1;i<len/2;++i,--j)
{
if(a[j]!=a[i])
flag=0;
}
if(flag==1)
cout<<"\nThe string is Palindrome";
else
cout<<"\nThe string is not Palindrome";
return 0;
}

Output-
Enter a string:saas
The string is Palindrome

6. Write a program to find largest number among three numbers


Ans #include <iostream>
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
// check if n1 is the largest number
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
// check if n2 is the largest number
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
// if neither n1 nor n2 are the largest, n3 is the largest
else
cout << "Largest number: " << n3;
return 0;
}

Output:
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

7. Write a program for print grade using switch statement


Ans#include <iostream>
using namespace std;
int main() {
int score, i, average;
float total=0;
cout<< "Enter marks of 5 subjects\n";
for(i=0; i<5; i++) {
cin >> score;
total += score;
}
average = total/5;
cout<<"Grade : ";
switch(average/10) {
case 9 :
cout << "A";
break;
case 8 :
case 7 :
cout << "B";
break;
case 6 :
case 5 :
cout << "C";
break;
default :
cout << "D";
}

return 0;
}

Output
Enter marks of 5 subjects
97 89 78 87 68
Grade : B

8.Write a program for nested switch statement.


Ans#include <iostream>
using namespace std;
int main()
{
int x = 1, y = 2;
// Outer Switch
switch (x) {
// If x == 1
case 1:
// Nested Switch
switch (y) {

// If y == 2
case 2:
cout << "Choice is 2";
break;

// If y == 3
case 3:
cout << "Choice is 3";
break;
}
break;

// If x == 4
case 4:
cout << "Choice is 4";
break;

// If x == 5
case 5:
cout << "Choice is 5";
break;

default:
cout << "Choice is other than 1, 2 3, 4, or 5";

}
return 0;
}
Output:
Choice is 2

9. Write the program and syntax for if else statement.


Ans// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number

#include <iostream>
using namespace std;

int main() {

int number;

cout << "Enter an integer: ";


cin >> number;

if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}

cout << "This line is always printed.";

return 0;
}
Output:
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.

You might also like