Assignment_1[1]
Assignment_1[1]
Assignment 1
Date: 17/01/2025
Sol ) Code:
#include <iostream.h>
int x;
main() {
int x = 10;
cout <<< : : x;
cout <<< x;
}
Key Observations:
1. Syntax Issues:
o The #include <iostream.h> header is not valid in modern C++. It should be #include
<iostream>.
o The main() function is missing a return type. It should be int main().
o The insertion operator << is incorrectly written as <<<. This would cause a compilation error.
2. Scope of x:
o The int x declared at the global scope is uninitialized and holds an undefined value.
o Inside main, a local variable x is declared and initialized to 10. This x shadows the global x
within the main function's scope.
3. Incorrect Use of :::
o The ::x syntax is used to access the global variable x in C++. However, it is being used
incorrectly in combination with the broken syntax of <<<.
4. Output Issues:
o Even if the syntax were corrected, accessing the uninitialized global variable x via ::x would
result in undefined behavior (UB). The output for this part would depend on the garbage value in
x.
Corrected Code:
#include <iostream>
using namespace std;
int main() {
int x = 10; // Local variable
cout << ::x << endl; // Access global x (uninitialized, may give 0 or
garbage based on the compiler)
cout << x << endl; // Access local x (10)
return 0;
}
3. Write 4 differences between Procedure oriented programming and object
oriented Programming.
4. Define the following i) Object ii) Class iii) Polymorphism
i) Object
ii) Class
A class is a blueprint or template used to create objects. It defines the structure and behavior of
the objects by specifying their attributes (data members) and methods (member functions).
However, a class by itself does not occupy memory until an object is created.
Syntax:
class ClassName {
public:
// Data members
// Member functions
};
Example:
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car myCar; // Object creation
myCar.brand = "BMW";
myCar.year = 2022;
myCar.display(); // Calling a method
return 0;
}
Polymorphism is a key concept in OOP that allows methods or functions to behave differently
based on the object or data type. It means "many forms" and enables the same function or
method to work in various ways.
Types of Polymorphism:
OR
5. Which operators are used as Memory Management Operators? Write the
syntax ( NEW and delete operator)
6. List the different access specifier with example
Access Specifiers
1. Public: Members can be accessed from anywhere.
2. Private: Members can be accessed only within the class.
3. Protected: Members can be accessed within the class and derived classes.
Example:
#include <iostream>
using namespace std;
class MyClass {
public:
int publicVar; // Accessible from anywhere
private:
int privateVar; // Accessible only within the class
protected:
int protectedVar; // Accessible within the class and derived classes
};
int main() {
MyClass obj;
obj.publicVar = 10; // Accessible because it's public
// obj.privateVar = 20; // Error: private member
// obj.protectedVar = 30; // Error: protected member
Output:
Public Variable: 10
7. What is Encapsulation? Explain with example.
Encapsulation is one of the fundamental principles of OOP that involves bundling the data (variables) and the
methods (functions) that operate on the data into a single unit, usually a class.
#include <iostream>
using namespace std;
class Student {
private:
int rollNo; // Private data member
string name;
public:
// Setter methods to set the values
void setRollNo(int r) {
rollNo = r;
}
void setName(string n) {
name = n;
}
string getName() {
return name;
}
int main() {
Student s1;
return 0;
}
8. List the types of Inheritance. Explain with examples.
1. Single Inheritance
In single inheritance, a derived class inherits from only one base class.
Syntax:
class Base {
// Base class members
};
Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
return 0;
}
2. Multiple Inheritance
In multiple inheritance, a derived class inherits from more than one base class.
Syntax:
class Base1 {
// Base class 1 members
};
class Base2 {
// Base class 2 members
};
Example:
#include <iostream>
using namespace std;
class Father {
public:
void height() {
cout << "Height inherited from Father." << endl;
}
};
class Mother {
public:
void eyeColor() {
cout << "Eye color inherited from Mother." << endl;
}
};
int main() {
Child c;
c.height(); // Inherited from Father
c.eyeColor(); // Inherited from Mother
c.talent(); // Defined in Child
return 0;
}
3. Multilevel Inheritance
In multilevel inheritance, a derived class inherits from a base class, and then another class
inherits from the derived class.
Syntax:
class Base {
// Base class members
};
Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited from Animal
d.walk(); // Inherited from Mammal
d.bark(); // Defined in Dog
return 0;
}
4. Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from a single base class.
Syntax:
class Base {
// Base class members
};
Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
int main() {
Dog d;
Cat c;
d.eat();
d.bark();
c.eat();
c.meow();
return 0;
}
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. It often leads to the
diamond problem, where ambiguity arises if a derived class inherits the same base class
through multiple paths. This is resolved in C++ using virtual inheritance.
Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
int main() {
Bat b;
b.eat(); // No ambiguity due to virtual inheritance
return 0;
}
Summary Table:
Type of Inheritance Description Example
Single Inheritance One derived class inherits from one base class. Animal → Dog
Multiple Inheritance One derived class inherits from multiple base classes. Father, Mother → Child
Multilevel Inheritance A chain of inheritance. Animal → Mammal → Dog
Hierarchical Inheritance Multiple derived classes inherit from one base class. Animal → Dog, Cat
Hybrid Inheritance Combination of two or more inheritance types. Animal → Mammal, Bird → Bat
Example:
Class
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
Structure
struct Car {
string brand;
int year;
void display() { // C++ struct can have methods.
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};