Introduction To C++ (Part1)
Introduction To C++ (Part1)
Mission 2023
OBJECT ORIENTED SYSTEM
DESIGN(OOSD)
UNIT- 4
UNIT 5 SYLLABUS
Objects and Classes : Basics of Object and class in C++, Private and public
members, static data and function members, constructors and their types, destructors,
operator overloading, type conversion. Inheritance : Concept of Inheritance, types of
inheritance: single, multiple, multilevel, hierarchical, hybrid, protected members,
overriding, virtual base class
Polymorphism : Pointers in C++, Pointes and Objects, this pointer, virtual and pure
virtual functions, Implementing polymorphism
OBJECT AND CLASS C++
OBJECTS AND CLASS IN C++
Encapsulation
Combines data and behavior into an entity
that we call an object. Furthermore,
objects isolate and protect the data from
the application or client program (the code
that uses the object). Objects allow the
application to access or use the data
through a stable, controlled public
interface formed by the class's public
functions.
ATTRIBUTES AND FEATURES
Class attributes are data or variables that instances of the class are responsible for maintaining
and upon which the instances operate. In general, an attribute is a quality or characteristic
inherent in something - some aspect whose values distinguish between different instances of the
thing. In the context of object-oriented programs, attributes are data that differentiate between
two instances of a class. So, while a class specifies the data (names and types) that each
instance has, the individual instances (i.e., the objects) may have different values for those data.
For example, a Person class might specify three attributes:
string name;
double height;
int weight;
Two instances of class Person (i.e., two Person objects) could describe two different people. Both
instances would have a name, a height, and a weight, but each instance would have distinct
variables to store its attribute values. So the two instances could represent two very different
people: two different names and one person could be tall and heavy while the other is short and
light. In C++, attributes are typically called member variables or member fields.
OPERATIONS AND BEHAVIOURS
• The private keyword restricts visibility and access to within the defining
class.
• The effects of the protected can't be described or appreciated until
inheritance is presented.
• The public keyword allows feature access, through an object,
throughout a program.
C++ CLASS SPECIFICATION
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
Time();
Time(int h, int m, int s);
Time(int s);
Time add(Time t2);
Time* add(Time* t2);
void print();
void read();
};
C++ CLASS SPECIFICATION
Q #include <iostream>
Company Based Objective Type Questions : -
using namespace std;
#define X 5
int main() {
int n = 10;
X = n; // LINE-1
cout << X;
return 0;
} What will be the output/error ?
A
5
B
10
C
0
D Compilation error at LINE-1: lvalue required as left operand of assignment.
MCQ
Ans Answer
Company Based Objective Type Questions : -
A
5
B
10
C
0
D
Compilation error at LINE-1: lvalue required as left operand
of assignment.
Explanation Manifest constants, declared as #define in a program,
are considered as constant throughout the program.
So, this rule is violated at LINE-1 which gives
compilation error.
MCQ
Ans Answer
Company Based Objective Type Questions : -
A
5
B
10
C Compilation error at LINE-1: uninitialized const 'p'.
D Compilation error at LINE-2: assignment of read-only variable 'p'.
Ans Answer
Company Based Objective Type Questions : -
A complex operator+(complex &c1, complex &c2)
B complex operator+(const complex &c1, const complex &c2)
C Compilation error at LINE-1: uninitialized const 'p'.
D Compilation error at LINE-2: assignment of read-only variable 'p'.
Q #include <iostream>
Company Based Objective Type Questions : -
using namespace std;
int main() {
int a = 5;
int &b = a;
++a; ++b;
a = a + b;
cout << a;
return 0;
}
A 10
B 11
C 13
D 14
MCQ
Ans Answer
Company Based Objective Type Questions : -
A 10
B 11
C 13
D 14
Q #include <iostream>
Company Based Objective Type Questions : -
using namespace std;
void fun(int a = 0) { cout << "1st" << endl; }
void fun() { cout << "2nd" << endl; }
int main() {
fun(); // LINE-1
return 0;
}
What will be the output/error of the above code?
A 1st
B 2nd
C 1st
2nd
D Compilation error at LINE-1: call of overloaded fun() is ambiguous.
MCQ
Ans Answer
Company Based Objective Type Questions : -
A 1st
B 2nd
C 1st
2nd
D Compilation error at LINE-1: call of overloaded fun() is ambiguous.
Q #include <iostream>
Company Based Objective Type Questions : -
using namespace std;
#define MUL(x , y) x * y
int main() {
int a = 10, b = 5, c, d;
c = MUL(a, b + 1);
d = MUL(a + 1, b);
cout << c << " " << d;
return 0; }
A 60 55
B 51 15
C 60 15
D 51 55
MCQ
Ans Answer
Company Based Objective Type Questions : -
A 60 55
B 51 15
C 60 15
D 51 55
Ans Answer
Company Based Objective Type Questions : -
B
const int *a = new int [2] { 5 , 10};
D const int *a = new int [2] { 5 , 10};
Explanation a is a pointer to a constant integer - eventually to an array of two int as
dynamically allocated.
It should be initialized during declaration for the desired output. So, LINE-
1 needs to be
modied as const int *a = new int[2] { 5, 10 };.