0% found this document useful (0 votes)
22 views24 pages

Introduction To C++ (Part1)

The document outlines the syllabus for a technical training unit on Object Oriented System Design (OOSD) using C++. It covers key concepts such as objects and classes, inheritance, polymorphism, encapsulation, and class specifications, along with multiple-choice questions to assess understanding. Additionally, it includes explanations for the answers to the questions, reinforcing the learning objectives.

Uploaded by

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

Introduction To C++ (Part1)

The document outlines the syllabus for a technical training unit on Object Oriented System Design (OOSD) using C++. It covers key concepts such as objects and classes, inheritance, polymorphism, encapsulation, and class specifications, along with multiple-choice questions to assess understanding. Additionally, it includes explanations for the answers to the questions, reinforcing the learning objectives.

Uploaded by

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

Technical Training

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

 Classes can define four kinds of operations (or functions in


C++)
 Constructors and destructors (create and destroy objects)
 Access (gets or sets private data values)
 Algorithmic (performs useful calculations with an object's
data)
 Helper (often private, extracts common code from the first
three operations or breaks out code to keep the other
operations from becoming too large)
FEATURE VISIBILITY

 C++, like Java, controls the visibility of


class features with a set of keywords (in
order of increasing visibility): private,
protected, and public.
 Although the C++ syntax is a little
different than Java, the meanings of the
keywords are the same in both
languages.

• 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

 C++ class, like a C++ struct, is terminated with a


semicolon. The only real difference between a struct and a
In this example, only function prototypes are included in the
class is the default visibility: structs have public visibility, class. Programmers frequently specify a single class in a header
and classes have private visibility. Nevertheless, C++ (i.e., .h) file named after the contained class, and write the
programmers generally only use structs to represent complete functions (including their bodies) in a separate .cpp
packaged data and reserve classes for situations that are file, also named for the class. Organizing classes this way
truly object-oriented (i.e., when an entity should have both makes them easier to reuse. This organization is the same as
attributes and operations packaged together).
introduced with structs in chapters 5 and 6.
 The public and private sections can appear in any order.
Like a struct, the name of a class becomes a type specifier,
Furthermore, a class may have any number of public and
private sections in any order. Any features following the
which means that it is a new, programmer-created data type.
private keyword label will have private visibility until The class name may appear wherever a program requires the
another label changes the visibility; similarly, any features name of a data type, specifically in a variable definition
following the public keyword label will have public statement:
visibility until another label changes the visibility.
MCQ

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'.

Explanation Here, the pointer p is constant. So, we have to initialize the


pointer while declaring. So, we will get compilation error at
LINE-1.
As p is constant pointer, the value can't be changed after
declaration. So, there is compilation error at LINE-2.
MCQ

Q #include<iostream> using namespace std;


Company BasedintObjective Type Questions : -
struct complex{ re, im;
void print(){ cout << re << "+i" << im; }
};
_______________________________{ //Line-1
struct complex c3={0,0};
c3.re = c1.re+c2.re; c3.im = c1.im+c2.im; return c3; }
int main(){
struct complex c1={2,5},c2{3,-2};
struct complex t = c1 + c2; t.print();
return 0;} Complete operator overloading for structure complex at Line-1 so that
the output is "5+i3".
A complex operator+(complex &c1, complex &c2)
B complex operator+(const complex &c1, const complex &c2)
C operator+(complex &c1, complex &c2)
D complex +(complex &c1, complex &c2)
MCQ

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'.

Explanation We need to overload addition operator for the structure complex. It


can be done as
complex operator+(complex &c1, complex &c2)
or,
complex operator+(const complex &c1, const complex &c2)
MCQ

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

Explanation As b is a reference of a, both variables point to same


memory location. So, ++a and ++b increment the initial
value of a two times and become 7.
Now, this value '7' is for both the variables a and b. So, the
equation is substituted as a = 7 + 7. The result is 14.
MCQ

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.

Explanation The call of fun() is ambiguous due to the default parameter


in the first definition and no parameter in second definition.
So, it will give compilation error at LINE-1.
FUNCTION IN C++
MCQ

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

Explanation Inline function substitutes all variables used by it before


compilation. So, MUL(a,b+1) will be substituted as a*b+1
which is 10*5+1 = 50 + 1 = 51. Similarly, MUL(a+1,b) will
be
substituted as a+1*b which is 10+1*5 = 10 + 5 = 15.
FRIEND FUNCTION

#include <iostream> // Note: printWidth() is not a member function of any class.


using namespace std;
class Box { void printWidth( Box box ) {
double width; /* Because printWidth() is a friend of Box, it can
directly access any member of this class */
public: cout << "Width of box : " << box.width <<endl;
friend void printWidth( Box box ); }
void setWidth( double wid );
}; int main() {
// Member function definition Box box;
void Box::setWidth( double wid ) { // set box width without member function
width = wid; box.setWidth(10.0);
} // Use friend function to print the wdith.
printWidth( box );
return 0;
}
MCQ

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 };.

You might also like