OOPS
OOPS
OOPS
ANS:-
The OOP, can be considered to be a type of structured
programming which uses structured programming techniques
for program flow, but adds more structure for data to be
modelled. We have highlighted some of the basic differences
between the two as under:
1) OOP is closer to the phenomena in real world due to the
use of objects whereas structured programming is a bit away
from the natural way of thinking of human beings.
ANS:-
The mechanism of information hiding is said to provide a
strictly controlled access to the information enclosed within
the capsule. Encapsulation is the process of enclosing within
classes and objects the attributes and the methods. But
information hiding cannot be treated as encapsulation, it is
different e.g. an array or a record structure also encloses the
information but this information cannot be said to be hidden.
It is true that the encapsulation mechanism like classes and
objects hide information but these also provide visibility of
some of their information through well defined interfaces
QUES:- DEFINE OOPS CONCEPTS ?
ANS:-
POLYMORPHISM:-
The word polymorphism means having many forms. In simple
words, we can define polymorphism as the ability of a
message to be displayed in more than one form.
A person at the same time can have different characteristic.
Like a man at the same time is a father, a husband, an
employee. So the same person posses different behaviour in
different situations. This is called polymorphism.
An operation may exhibit different behaviours in different
instances. The behaviour depends upon the types of data
used in the operation.
C++ supports operator overloading and function overloading.
• Operator Overloading: The process of making an
operator to exhibit different behaviours in different instances
is known as operator overloading.
• Function Overloading: Function overloading is using a
single function name to perform different types of tasks.
Polymorphism is extensively used in implementing
inheritance
• Example: Suppose we have to write a function to add
some integers, some times there are 2 integers, some times
there are 3 integers. We can write the Addition Method with
the same name having different Parameters, the concerned
method will be called according to parameters.
INHERITENCE:-
UNIT 2 INTRODUCTION TO C+ +
Explicit Conversion
When the user manually changes data from one type to
another, this is known as explicit conversion. This type of
conversion is also known as type casting.
5/2= 2
(float)5/2=2.5
LITERALS OR CONSTANTS
* Arithmetical operators
* Relational operators
* Logical operators
* Bitwise operators
* Precedence of operators
* Special operators
* Arithmetical operators :-
/ division 10/2 5
% modulo 5%2 1
Relational operators
Bitwise Operators
Precedence of Operators
a = 5 + 7 % 2 Here we may doubt if it really means:
A = 5 + (7 % 2) // with a result of 6, or a = (5 + 7) % 2 // with a
result of 0
The correct answer is the first of the two expressions, with a
result of 6.
Special Operators
Increment and Decrement Operator
++,--
class abstraction
{
private:
int x, y;
public:
void display()
{
cout<<"x = " <<x << endl;
cout<<"y = " << y << endl;
}
};
int main()
{
abstraction sp;
sp.set(20, 30);
sp.display();
return 0;
}
Output:
Sum = 95
In the program above, you can see that we are not allowed to
directly access the variables x and y. But you can call the set()
function to set the values x and y. The display() function is
called to display the values x and y.
Advantages of data abstraction
• It prevents the user from writing low-level code.
• It prevents duplication of software and increases
reusability.
• The internal class implementation can be altered
without impacting the user.
• It helps to improve the privacy of an application or
program as the user is only presented with relevant
information.
Characteristics:-
• The function is not in the scope of the class to which it
has been declared as a friend.
• It cannot be called using the object as it is not in the
scope of that class.
• It can be invoked like a normal function without using
the object.
• It cannot access the member names directly and has to
use an object name and dot membership
• operator with the member name.
example
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. private:
6. int length;
7. public:
8. Box(): length(0) { }
9. friend int printLength(Box); //friend function
10. };
11. int printLength(Box b)
12. {
13. b.length += 10;
14. return b.length;
15. }
16. int main()
17. {
18. Box b;
19. cout<<"Length of box: "<< printLength(b)<<endl;
20. return 0;
21. }
Output:
Length of box: 10
Ques:- Define static member?
Ans:- static members are data members (variables) or
methods that belong to a static or a non static class itself,
rather than to objects of the class. Static members always
remain the same, regardless of where and how they are used.
Because static members are associated with the class, it is
not necessary to create an instance of that class to invoke
them.
Since a static variable is associated with the class, only one
copy of the variable exists in memory. This copy is shared by
all objects of that class.
Some of the features of static members are as follows:
• A static member has access to all static members of its
containing class, including private members.
• A static member can be declared using access control
modifiers.
• A static member class can use any other static member
without qualifying its name with the name of the containing
class.
Properties of static member functions:
1. A static function can only access other static variables or
functions present in the same class
2. Static member functions are called using the class name.
Syntax- class_name::function_name( )
example
#include <iostream>
class Example{
static int Number;
int n;
public:
void set_n(){
n = ++Number;
}
void show_n(){
cout<<"value of n = "<<n<<endl;
}
int main()
{
Example example1, example2;
example1.set_n();
example2.set_n();
example1.show_n();
example2.show_n();
Example::show_Number();
return 0;
}
Output:
From the above output, we can see that the value of the
variable ‘n’ is different for both the objects ‘example1’ and
‘example2’ of the class ‘Example’. Since the variable ‘Number’
is a class variable its value is the same for both the objects
‘example1’ and ‘example2’. Static member variables and
functions are used when common values are to be shared
across all the objects. While programming, the use of static
keyword should be done wisely.
# include<iostream.h>
# include<conio.h>
class student
{
public:
student() // user defined constructor
{
cout<< “object is initialized”<<end1;
}
};
void main ()
{
student x,y,z;
getch();
}
int main()
{
student s; // default construtor
s.show_data();
student A(s); // copy constructor
A.show_data();
getch();
return 0;
}
QUES: DEFINE CONSTRUCTOTR OVERLOADING?
ANS:-
Constructor Overloading When more than one constructor
function is defined in a class, then it is called constructor
overloading or use of multiple constructor in a class. It is used
to increase the flexibility of a class by having more number of
constructors for a single class. Overloading constructors in C+
+ programming gives us more than one way to initialize
objects in a class.
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int roll;
char name[30];
public:
student(int x, char y[]) // parameterized constructor
{
roll =x;
strcpy(name,y);
}
student() // normal constructor
{
roll =100;
strcpy(name,"y");
}
void input_data()
{
cout<<"\n Enter roll no :"; cin>>roll;
cout<<"\n Enter name :"; cin>>name;
}
void show_data()
{
cout<<"\n Roll no :"<<roll;
cout<<"\n Name :"<<name;
}
};
int main()
{
student s(10,"z");
s.show_data();
getch();
return 0;
}
#include <iostream>
using namespace std;
class DemoDC {
private:
int num1, num2 ;
public:
DemoDC() {
num1 = 10;
num2 = 20;
}
void display() {
cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl;
}
};
int main() {
DemoDC obj;
obj.display();
return 0;
}
Output
num1 = 10
num2 = 20
class Person {
private:
int age;
public:
// 1. Constructor with no arguments
Person() {
age = 20;
}
int getAge() {
return age;
}
};
int main() {
Person person1, person2(45);
return 0;
}
Output
Person1 Age = 20
Person2 Age = 45
QUES:- EXPLAIN WITH EXAMOPLE CONTINUE,BREAK,EXIT
GOTO STMTS
ANS:-
In C++, the break statement terminates the loop when it is
encountered.
The syntax of the break statement is:
break;
example
// program to print the value of i
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// break condition
if (i == 3) {
break;
}
cout << i << endl;
}
return 0;
}
output
1
2
continue stmt:-
n computer programming, the continue statement is used to
skip the current iteration of the loop and the control of the
program goes to the next iteration.
The syntax of the continue statement is:
continue;
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// condition to continue
if (i == 3) {
continue;
}
return 0;
}
Output
1
2
4
5
exit stmt
goto stmt:-
In C++ programming, the goto statement is used for altering
the normal sequence of program execution by transferring
control to some other part of the program.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. ineligible:
6. cout<<"You are not eligible to vote!\n";
7. cout<<"Enter your age:\n";
8. int age;
9. cin>>age;
10. if (age < 18){
11. goto ineligible;
12. }
13. else
14. {
15. cout<<"You are eligible to vote!";
16. }
17. }
int main () {
for( ; ; ) {
printf("This loop will run forever.\n");
}
return 0;
}
example of for loop
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
cout << " Good morning \n";
}
return 0;
}
example of while loop
22
You are eligible to vote!
return 0;
}
#include <iostream>
using namespace std;
inline int cube(int x)
{
return x*x*x;
}
int main()
{
cout << "Our cube is: " << cube(3) << "\n";
return 0;
}
ques:- define scope resolution operator in c++?
ans:- i) to distinquish between local and global variable
ii) to associate member function outside a class
#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i=0; i<6; i++)
{
for(j=0; j<=i; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}
output