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

CPP Questions

cpp question

Uploaded by

kishorgaikar876
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CPP Questions

cpp question

Uploaded by

kishorgaikar876
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

what is pointer :
A pointer is a variable that stores the memory address of another variable. It allows you to directly
access and manipulate the memory loca�on.

Exampale:

#include <stdio.h>

int main() {

int num = 42;

int *ptr = &num;

prin�("Value of num: %d\n", num);

prin�("Address of num: %p\n", (void*)&num);

prin�("Value at ptr: %d\n", *ptr); /

return 0;

2 ]write a 2 number
swap : #include <iostream>

using namespace std;

int main()

int a = 5, b = 10;

cout << "Before swapping." << endl;

cout << "a = " << a << ", b = " << b << endl;

a = a + b;

b = a - b;

a = a - b;

cout << "\nA�er swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;

write a program to calculate two string by using


#3]

pointer:
#include <iostream>
using namespace std;

void concatenate(char* str1, char* str2, char* result) {


while (*str1) {
*result++ = *str1++;
}
while (*str2) {
*result++ = *str2++;
}
*result = '\0'; // Null terminate the result
}
int main() {
char str1[100], str2[100], result[200}
cout << "Enter first string: ";
cin.getline(str1, 100);
cout << "Enter second string: ";
cin.getline(str2, 100)
concatenate(str1, str2, result);
cout << "Concatenated string: " << result << endl;

#4 write a syntax for overloaded constructor :


class ClassName {
public:
parameters
ClassName()
}
one parameter
ClassName(int a) {
}
two parameters
ClassName(int a, int b)
}
};
#5.Explain any four rules for virtual function
Declared in Base Class:
A virtual func�on must be declared in the base class, typically
using the virtual keyword. Derived classes can override it.

2. Access via Pointer/Reference:


A virtual func�on is used to achieve run�me
polymorphism.
To ensure that the correct version (base or derived) of the
func�on is called, the func�on must be accessed via a
pointer
or reference to the base class.
3. Base Class Destructors Should be Virtual:
If a class has virtual func�ons, its destructor should also be
virtual to ensure that the destructor of derived classes is
called when dele�ng an object via a pointer to the base class.
4. Cannot be Sta�c:
Virtual func�ons cannot be sta�c members. Sta�c func�ons
are not associated with any object instance, while virtual
func�ons are resolved dynamically at run�me based on
the object's type.
6]what is pure func�on:
. Always returns the same result for the same input.
2. Has no side effects, meaning it doesn't modify external
variables or states.
7] define polymorphisum:
1. Compile-�me (Sta�c) Polymorphism:
Achieved through func�on overloading and operator
overloading, where the decision of which func�on to call
is made during compila�on.

Func�on Overloading: Mul�ple func�ons with the same


name but different parameters.
Operator Overloading: Defining custom behavior for
operators for user-defined types.
2. Run-�me (Dynamic) Polymorphism:
Achieved through inheritance and virtual func �ons,
where the func�on call is resolved at run�me based on
the actual object's type.
8] what is call by value :
What is call by address:
Call by Value:
A copy of the actual parameter is passed to the func�on.
Changes made inside the func�on do not affect the original
variable.
void modify(int x) {
x = 10; // changes local copy only
}

Call by Address:
The address of the actual parameter is passed to the
func�on.
Changes made inside the func�on affect the original variable.
void modify(int* x) {
*x = 10; // changes the original value
}

You might also like