SEE 2073 (Supplementary)
Group A
1. Write down the syntax of do-while loop
à Syntax:
do {
// statement(s)
} while( condition );
2. Define recursive function.
à A recursive function is a function that calls itself during its execution.
3. What do you mean by members of class in C++?*
à Members of class in C++ means the function and variable which are inside the class.
4. Define array.
à Array is a collection of similar data items stored at contiguous memory locations.
5. What is operator overloading?
à The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
6. Define streams.
à Streams are sequence of bytes acting as a channel between the processor and the input/output devices.
Group B
7. Explain the term 'code-reusability' in object-oriented programming.
à "Code reusability” can be achieve through inheritance. In OOP, when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
8. Why do we write comment in programming? Explain with an example.
à We write comment in programming to provide information or explanation about the variable, method, class, or any statement so that other people can understand it easily.
Example:
#include<iostream>
using namespace std;
int main(){
cout<<"Hello world"; //Prints hello world, in this way we can write comment
return 0;
}
9. Write down any to differences between while loop and do-while loop.
Ã
10. How function can be overloaded?
à A function can be overloaded by:
· Changing in number of arguments
· Changing in type of arguments.
11. Explain static local variable with example.*
Ã
12. Define constructor with its syntax.
à A class constructor is a special function in a class that is called automatically whenever a new object of the class is created.
Syntax:
class_name(arguments){
//statements
}
13. Write any two differences between class and structures.
Ã
14. WAP to read a set of elements of an array and display the result at the standard output device.
Ã
#include <iostream>
using namespace std;
int main() {
int numbers[5], i, n ;
cout << "Enter 5 numbers: " << endl;
// store input from user to array
for (i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
// print array elements
for (n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;
}
15. Write down the differences between single-dimensional array and multi-dimensional array.
Ã
Single-dimension | Multi-dimensional |
Store data as a list | Stores data in row-column format. |
Syntax: datatype variable_name[row] | Syntax: datatype variable_name[row][column] |
16. Describe the syntax of operator function.
à Syntax: Return_Type classname :: operator operatpr_symbol(parameters list){
// Function Body }
The syntax of operator function is similar to that of function, just replace the function_name with operator keyword and symbol of operator.
17. What is the purpose of inheritance in C++?
à Purpose of inheritance in C++ are:
· Code Reusability
· Method Overriding (Hence, Runtime Polymorphism.)
· Use of Virtual Keyword
18. Draw the figure of stream class hierarchy.*
Ã
19. Write down the methods of file opening in C++.*
Ã
· Using parameterized constructor
· Using member function
Group C
20. WAP to find the largest number among the four numbers input through the keyboard.
à Program to find the largest number among the four numbers input through the keyboard:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cout<<"Enter four numbers : ";
cin>>a>>b>>c>>d;
if(a>=b && a>=c && a>=d)
cout<<a;
else if(b>=a && b>=c && b>=d)
cout<<b;
else if(c>=a && c>=b && c>=d)
cout<<c;
else
cout<<d;
cout<<" is Largest.";
return 0;
}
21. WAP to find the factorial of any integer number.
Ã
#include <iostream>
using namespace std;
int main() {
int n,factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
22. Explain copy constructor with an example
à The copy constructor is a constructor which creates an object by initializing it with an object of the same class. The copy constructor in C++ is used to copy data of one object to another.
If a copy constructor is not defined in a class, the compiler itself defines one.If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor. The most common form of copy constructor is shown here
− classname (const classname &obj) { // body of constructor }
Here, obj is a reference to an object that is being used to initialize another object.
Example:
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
int length;
int height;
public:
// initialize variables with parameterized constructor
Wall(int len, int hgt) {
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
int calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10, 8);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
Output:
Area of Wall 1: 80
Area of Wall 2: 80
23. WAP to read a set of numbers and store them in an array and multiply all the elements.
Ã
#include<iostream>
using namespace std;
int main(){
int a[25],n,i,mul=1;
cout<<"Enter the size of array : ";
cin>>n;
cout<<"Enter the elements of array :";
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
mul=mul*a[i];
}
cout<<"the multiplication of elements of array : "<<mul;
return 0;
}
24. Write a program to load unary operator.
Ã
C++ program to show unary operator overloading:
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count(int a ){
value=a;
}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1(5);
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output:
Count: 6
In the above program, it shows that no argument is passed and no return_type value is returned, because unary operator works on a single operand.
25. Write any five differences between constructor and member functions.
Ã
Member function | Constructor |
Member function has return type. | Constructor doesn’t have return type |
Can have any name other than keywords | Must have same name as class name |
Member function need to be called by using object of the class | Constructor is called automatically when new object of the class is created. |
There is no default function provided by compiler | There is always a default constructor provided by compiler if programmer doesn’t write the constructor |
26. Write a program to write a message "Hello world" in a file name "xyz.txt
Ã
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream newfile; //fstream class
newfile.open("xyz.txt",ios::out); // open a file to perform write operation using file object
if(newfile.is_open()) //checking whether the file is open
{
newfile<<"Hello World!"; //inserting text
newfile.close(); //close the file object
}
return 0;
}
0 Comments