Bca 3rd Sem Oops
Bca 3rd Sem Oops
● Example:
● Example:
int x;
cin >> x;
● Example (C):
#include <stdio.h>
void sayHello() {
printf("Hello, World!");
}
int main() {
sayHello();
return 0;
}
Important Points :-
Example(C++):-
#include <iostream>
class Person {
private:
string name;
public:
void setName(string n) {
name = n;
}
void introduce() {
cout << "Hello, my name is " << name <<endl;
}
};
int main() {
Person p;
p.setName("John");
p.introduce();
return 0;
}
Q3. Define Classes and Objects. Explain the Concept of Base and
Derived Class using an Example?
Ans.
Classes:- A class in object-oriented programming is a user-defined data
type that serves as a blueprint for creating objects. It defines attributes
(data) and methods (behaviour/Functions) that the objects instantiated
from the class will possess. The class itself doesn't hold data, but it
provides a structure for objects, which are specific instances of that
class.
void accelerate() {
speed += 10;
}
};
int main() {
Car myCar; // Creating an object 'myCar' of class 'Car'
myCar.brand = "Swift";
myCar.speed = 100;
myCar.accelerate();
return 0;
}
// Base Class
class Animal {
public:
void eat() {
cout << "This animal eats food." <<endl;
}
};
// Derived Class
class Dog : public Animal { // Dog inherits from Animal
public:
void bark() {
cout << "The dog barks." <<endl;
}
};
int main() {
Dog myDog; // Create object of Derived Class 'Dog'
myDog.eat(); // Inherited from Animal class
myDog.bark(); // Method of Dog class
return 0;
}
Q5. Define Array and its Type. Explain One Dimensional Array?
Ans.
Array:- An array is a collection of variables of the same data type that
are stored in contiguous memory locations. It allows multiple values to
be stored under a single variable name, and each element in the array
can be accessed using an index. Arrays are useful for storing and
managing large amounts of data efficiently.
Types of Arrays:
1. One-Dimensional Array: A linear array where data is stored in a
single row or line.
2. Two-Dimensional Array: An array that stores data in rows and
columns, like a table or matrix.
3. Multi-Dimensional Array: An array with more than two
dimensions, often used in more complex scenarios like 3D arrays.
One-Dimensional Array:
A one-dimensional array is the simplest form of an array, where
elements are stored in a single row or line. You can think of it as a list of
elements stored one after another.
#include <stdio.h>
int main() {
int numbers[5]; // Declare an array of 5 integers
// Initializing elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
Flowchart:
A flowchart is a diagram that represents an algorithm, process, or
workflow. It shows steps in the form of boxes connected by arrows,
illustrating the sequence of operations. Flowcharts help visualize the
logic of a process.
FlowChart:
Types of Constructors:
Default Constructor: A constructor that takes no parameters.
class Outer {
public:
int outerVar;
// Nested class (Inner class)
class Inner {
public:
void display() {
cout << "I am the nested Inner class!" << endl;
}
};
void show() {
cout << "I am the outer class!" << endl;
}
};
int main() {
// Creating object of outer class
Outer outerObj;
outerObj.show();
return 0;
}
Q9. Explain static data members & statics data members function.
Ans. Static Data Members in C++
1. No Object Required:
○ Static member functions can be called using the class name
without creating an object.
2. Cannot Access Non-Static Members:
○ Since static functions do not belong to any specific object,
they can only access static members. They cannot access
non-static data members or functions because these are
tied to objects.
3. Access Static Data:
○ They are mainly used to manipulate static data members of
the class.
The word "polymorphism" comes from the Greek words "poly" (meaning
many) and "morph" (meaning forms), indicating that a single entity (like a
function or operator) can take on different forms.
● Function Overloading
● Operator Overloading
a) Function Overloading:
b) Operator Overloading:
Operator overloading allows operators like +, -, *, etc., to be redefined
so they work with objects of user-defined types (e.g., classes). This
provides a way to extend the meaning of operators for custom types.
● Virtual Functions
● Function Overriding
a) Virtual Functions:
b) Function Overriding:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
1. Single Inheritance
In single inheritance, one class inherits from a single base class. The
derived class gets access to the properties and methods of the base
class.
2. Multiple Inheritance
In multiple inheritance, a derived class inherits from more than one base
class. This means the derived class can inherit properties and methods
from multiple base classes.
3. Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class,
creating a chain of inheritance levels.
4. Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from the same
base class. This means multiple classes share the same parent class.
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance.
For example, it can be a combination of multiple and hierarchical
inheritance.
1. Pass by Value
2. Pass by Reference
3. Pass by Pointer
1. Pass by Value:
2. Pass by Reference:
3. Pass by Pointer: