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

Pyq Part 1

New notes

Uploaded by

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

Pyq Part 1

New notes

Uploaded by

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

Here are the solutions to the given C++ programming questions:

1. (a) Short Notes:


● Object: An instance of a class. It represents a real-world entity with its own state
(attributes) and behavior (methods).
● Class: A blueprint or template for creating objects. It defines the data members
(attributes) and member functions (methods) that objects of that class will have.
● Data Encapsulation: The practice of bundling data (attributes) and methods (functions)
that operate on that data within a single unit (class). This helps in protecting data integrity
and controlling access.
● Inheritance: The mechanism by which one class (child class or derived class) inherits the
properties and behaviors of another class (parent class or base class). This promotes
code reusability and creates hierarchical relationships between classes.
● Polymorphism: The ability of objects of different types to be treated as if they were of the
same type. It allows different objects to respond to the same message in different ways,
based on their specific implementations.
1. (b) Storage Classes:
● auto: Variable's storage class is determined by its declaration context. It's generally used
for local variables.
● register: Suggests to the compiler to store the variable in a register for faster access.
However, the compiler might not always honor this request.
● static: Variables declared as static retain their values between function calls and have a
longer lifetime than automatic variables. They can be used for global variables or within
functions.
● extern: Declares a variable that is defined elsewhere, typically in another file. It's used for
sharing variables between different files.
2. (a) Dereferencing Operator:
The dereferencing operator * is used to access the value stored at a memory location pointed to
by a pointer. It's essential for working with pointers and dynamically allocated memory.
Here's an example:
#include <iostream>

int main() {
int x = 10;
int *ptr = &x; // ptr points to the address of x

std::cout << *ptr << std::endl; // Prints the value of x, which is


10
}

2. (b) Control Structures in C++:


● Selection Statements:
○ if statement: Executes a block of code if a condition is true.
○ if-else statement: Executes one block of code if a condition is true, and another
block if it's false.
○ switch statement: Selects one of multiple code blocks to execute based on the
value of an expression.
● Iteration Statements:
○ for loop: Repeats a block of code a specified number of times.
○ while loop: Repeats a block of code as long as a condition is true.
○ do-while loop: Repeats a block of code at least once, and then continues as long as
a condition is true.
3. (a) Unary Operator Overloading:
#include <iostream>

class ComplexNumber {
public:
int real, imag;

ComplexNumber(int r = 0, int i = 0) : real(r), imag(i) {}

// Overload the unary - operator


ComplexNumber operator-() {
return ComplexNumber(-real, -imag);
}

void display() {
std::cout << real << " + " << imag << "i" << std::endl;
}
};

int main() {
ComplexNumber c1(3, 4);
ComplexNumber c2 = -c1;

std::cout << "Original complex number: ";


c1.display();

std::cout << "Negated complex number: ";


c2.display();

return 0;
}

3. (b) Student Class:


#include <iostream>
#include <string>

class Student {
public:
std::string name;
int rollNumber;
float gpa;

// Default constructor
Student() {
name = "Unknown";
rollNumber = 0;
gpa = 0.0;
}

// Parameterized constructor
Student(std::string n, int r, float g) {
name = n;
rollNumber = r;
gpa = g;
}

// Copy constructor
Student(const Student &s) {
name = s.name;
rollNumber = s.rollNumber;
gpa = s.gpa;
}

// Destructor
~Student() {
std::cout << "Student object destroyed: " << name <<
std::endl;
}

// ... other member functions ...


};

int main() {
// ...
}

Note: This is just a basic implementation. You can add more member functions like display, set,
get, etc. to the Student class as needed.
Please let me know if you have any other questions or require further assistance.

You might also like