Untitled 1
what is oop ?
==-Object-oriented programming (OOP) is a programming language model in which programs are organized around data, or objects, rather than functions and logic
== An object can be defined as a data field that has unique attributes and behavior.
————————————————————————————–
Principles of OOP
==Object-oriented programming is based on the following principles
==Encapsulation- The implementation and state of each object are privately held inside a defined boundary, or class.
Other objects do not have access to this class or the authority to make changes but are only able to call a list of public functions, or methods. This characteristic of data hiding provides greater program security and avoids unintended data corruption.
==Abstraction- Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. This concept helps developers make changes and additions over time more easily.
==Inheritance- Relationships and subclasses between objects can be assigned, allowing developers to reuse a common logic while still maintaining a unique hierarchy. This property of OOP forces a more thorough data analysis, reduces development time and ensures a higher level of accuracy.
==Polymorphism- Objects are allowed to take on more than one form depending on the context. The program will determine which meaning or usage is necessary for each execution of that object, cutting down on the need to duplicate code.
——————————————————————————–
Difference between OOP and POP:
OOP…..| POP
1.it is Object oriented…….it is Structure oriented.
2.the Program is divided into objects. ……..the Program is divided into functions.
3.it is Bottom-up approach……it is Top-down approach.
4.Inheritance property is used…… here Inheritance is not allowed.
5.It uses access specifier……. It doesn’t use access specifier.
6.the Encapsulation is used to hide the data…………… there isNo data hiding.
7.Concept of virtual function is there ………there is No virtual function.
8.exp ,C++, Java…….. exp,C, Pascal.
——————————————————————————–
Explain Basic Concepts of OOP. OR
Explain Following terms.
Various concepts present in OOP to make it more powerful, secure, reliable and easy.
—-Object
An object is an instance of a class.
An object means anything from real world like as person, computer, bench etc…
Every object has at least one unique identity.
An object is a component of a program that knows how to interact with other pieces of the program.
An object is the variable of the type class.
For example, If water is class then river is object.
—-Class
A class is a template that specifies the attributes and behavior of things or objects.
A class is a blueprint or prototype from which objects are created.
A class is the implementation of an abstract data type (ADT).
It defines attributes and methods.
—-Example:
class employee // Class
{
char name[10]; // Data member
int id; // Data member
public:
void getdata() // Member function
{
cout<”enter name=”” and=”” id=”” of=”” employee:=”” ”;=””>”enter>
cin>>name>>id;
}
}a; // Object Declaration
In above example class employee is created and ‘a’ is object of this class.
Object declaration can be also done in main() function as follows:
int main()
{
employee a;
}
—Data Abstraction
Just represent essential features without including the background details.
They encapsulate all the essential properties of the objects that are to be created.
The attributes are sometimes called ‘Data members’ because they hold information.
The functions that operate on these data are sometimes called ‘methods’ or ‘member functions’.
It is used to implement in class to provide data security.
—Encapsulation
Wrapping up of a data and functions into single unit is known as encapsulation.
In C++ the data is not accessible to the outside world.
Only those functions can access it which is wrapped together within single unit.
—Inheritance
Inheritance is the process, by which class can acquire the properties and methods of another class.
The mechanism of deriving a new class from an old class is called inheritance.
The new class is called derived class and old class is called base class.
The derived class may have all the features of the base class.
Programmer can add new features to the derived class.
For example, Student is a base class and Result is derived class.
—Polymorphism
-A Greek word Polymorphism means the ability to take more than one form.
- Polymorphism allows a single name to be used for more than one related purpose.
-The concept of polymorphism is characterized by the idea of ‘one interface, multiple methods’,
-That means using a generic interface for a group of related activities.
- The advantage of polymorphism is that it helps to reduce complexity by allowing one interface to specify a
general class of action’. It is the compiler’s job to select the specific action as it applies to each situation.
It means ability of operators and functions to act differently in different situations.
—Example:
int total(int, int);
int total(int, int, float);
==Static Binding
Static Binding defines the properties of the variables at compile time. Therefore they can’t be changed.
==Dynamic Binding
Dynamic Binding means linking of procedure call to the code to be executed in response to the call.
It is also known as late binding, because It will not bind the code until the time of call at run time. In
other words properties of the variables are determined at runtimes.
It is associated with polymorphism and inheritance.
==Message Passing
A program contains set of object that communicates with each other.
Basic steps to communicate
1. Creating classes that define objects and their behavior.
2. Creating objects from class definition
3. Establishing communication among objects.
Message passing involves the object name, function name and the information to be sent.
==Example: employee.salary(name);
In above statement employee is an object. salary is message, and name isinformation to be sent.\
List out benefits of OOP.
1.We can eliminate redundant code though inheritance.
2.Saving development time and cost by using existing module.
3.Build secure program by data hiding.
4.It. is easy to partition the work in a project based on object.
5.it.ata centered design approach captures more details of a programming model.
6.It .can be easily upgraded from small to large system.
7.It is easy to map objects in the problem domain to those in the program.
8.Through message passing interface between objects makes simpler description with external system.
9.Software complexity can be easily managed.
4) List out Applications of OOP.
this are the maain applications of oop
1. Real-time systems
2. Simulation and modeling
3. Object oriented database
4. Artificial intelligence and expert systems
5. Neural networks and parallel programming
6. Decision support and office automation
7. CIM/CAM/CAD systems.
8. Distributed/Parallel/Heterogeneous computing
9. Data warehouse and Data mining/Web mining
question-why c++ is note pure OOP langusge or why it is called partial or semi Object Oriented Language:
Main function is outside the class : C++ supports object-oriented programming, but OO is not intrinsic to the language. You can write a valid, well-coded, excellently-styled C++ program without using an object even once.
Concept of Global variable : In C++, we can declare a variable globally, which can be accessible from anywhere and hence, it does not provides complete privacy to the data as no one can be restricted to access and modify those data and so, it provides encapsulation partially whereas In JAVA, we can declare variable inside class only and we can provide access specifier to it.
Availability of Friend function: Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.
Therefore, again the Object oriented features can be violated by C++.
question –diferentiate c vs….. c++
1.C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming……..C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.
2.C is a subset of C++……..C++ is a superset of C.
3.C contains 32 keywords. …….C++ contains 52 keywords.
4.For the development of code, C supports procedural programming. …….C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms.
5.Data and functions are separated in C because it is a procedural programming language. ……Data and functions are encapsulated together in form of an object in C++. 6.C does not support information hiding. Data is hidden by the Encapsulation to ensure that data structures and operators are used as intended.
7.Built-in data types is supported in C.. ……Built-in & user-defined data types is supported in C++.
8.is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming.
9.unction and operator overloading is not supported in C. ……Function and operator overloading is supported by C++.
10.C is a function-driven language. …….C++ is an object-driven language
———————————————————————————————————————————————————————————————————————————————————
What is C++? Explain Structure of C++ Program.
~ C++ is an object oriented programming language.
~ It is a superset of C language and also called as extended version of C language.
~ It was developed by Bjarne Stroustrup at AT&T Bell lab in New Jersey, USA in the early 1980’s.
————————————————————————————–
~ Structure of C++ program is as follow.
Include Files
Class Declaration or Definition
Member functions definitions
Main function
~In any program first write header files like as iostream.h, conio.h, etc..as per requirement of program.
~After header file write class declaration or definition as per your planning.
~After class, define all member functions which are not define but declare inside the class.
~In last write main function without main function program execution is not possible.
—————————————————-
Example:
#include //Header File
using namespace std;
class trial //Class
{
int a;
public:
void getdata()
{
a=10;
}
void putdata();
};
void trial::putdata() //Member Function outside class definition
{
cout<“the value=”” of=”” a=”<}
int main() //Main Function
{
trial T;
T.getdata();
T.putdata();
}
~~Output:
The value of a = 10
—————————————————————————————————————————————————————————————————————–
“the> Describe various operators used in C++.
An operator is a symbol that tells the compiler to perform certain mathematical or logical operation.
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculation. C++ supports following arithmetic operators
+ Addition or unary plus
– Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
(note!!. make box on signs and than write its expression on right)
2. Relational Operators
Relational operators are used to compare two numbers and taking decisions based on their relation.
Relational expressions are used in decision statements such as if, for, while, etc…
< less> less>
<= less=”” than=”” or=”” equal=””>=>
> greater than
>= greater than or equal to
== is equal to
!= is not equal to
3. Logical Operators
Logical operators are used to test more than one condition and make decisions
&& logical AND (Both non zero then true, either is zero then
false)
|| logical OR (Both zero then false, either is non zero then true)
! logical NOT (non zero then false, zero then true)
4. Assignment Operators
Assignment operators are used to assign the result of an expression to a variable. C++ also supports
shorthand assignment operators which simplify operation with assignment
= Assigns value of right side to left side
+= a += 1 is same as a = a + 1
-= a -= 1 is same as a = a – 1
*= a *= 1 is same as a = a * 1
/= a /= 1 is same as a = a / 1
%= a %= 1 is same as a = a % 1
5. Increment and Decrement Operators
These are special operators in C++ which are generally not found in other languages.
++ Increments value by 1.
a++ is postfix, the expression is evaluated first and then the value is incremented.
Ex. a=10; b=a++; after this statement, a= 11, b = 10.
++a is prefix, the value is incremented first and then the expression is evaluated.
Ex. a=10; b=++a; after this statement, a= 11, b = 11.
— Decrements value by 1.
a– is postfix, the expression is evaluated first and then the value is decremented.
Ex. a=10; b=a–; after this statement, a= 9, b = 10.
–a is prefix, the value is decremented first and then the expression is evaluated.
Ex. a=10; b=–a; after this statement, a= 9, b = 9.
6. Conditional Operator
A ternary operator is known as Conditional Operator.
exp1?exp2:exp3 if exp1 is true then execute exp2 otherwise exp3
Ex: x = (a>b)?a:b; which is same as
if(a>b)
x=a;
else
x=b;
7. Bitwise Operators
Bitwise operators are used to perform operation bit by bit. Bitwise operators may not be applied to float
or double.
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
< shift=”” left=”” (=”” shift=”” left=”” means=”” multiply=”” by=””>
>> shift right ( shift right means divide by 2)
8. Special Operators
& Address operator, it is used to determine address of the variable.
* Pointer operator, it is used to declare pointer variable and to get value from it.
, Comma operator. It is used to link the related expressions together.
sizeof It returns the number of bytes the operand occupies.
. member selection operator, used in structure.
-> member selection operator, used in pointer to structure.
9. Extraction operator (>>)
Extraction operator (>>) is used with cin to input data from keyboard.
10. Insertion operator (<>
Insertion operator (<) is=”” used=”” with=”” cout=”” to=”” output=”” data=”” from=””>)>
11. Scope resolution operator (::)
Scope resolution operator (::) is used to define the already declared member functions of the class.
Explain use of scope resolution operator (::) by giving example.
– The scope resolution operator is used to resolve or extend the scope of variable.
– C++ is block structured language. We know that the same variable name can be used to have different
meaning in different block.
-The scope resolution operator will refer value of global variable from anywhere (also from inner block).
– Without scope resolution operator all variable will refer local value.
Example:
#include
int m=10;
int main()
{
int m=20;
{
int k=m;
int m=30;
cout<“we are=”” in=”” inner=””>”we>
cout<><><>
cout<><><>
cout<><><>
}
cout<“we are=”” in=”” outer=””>”we>
cout<><><>
cout<><><>
return 0;
}
Output:
we are in inner block
k=20
m=30
we are in outer block
m=20
::m=20
————————————————————————————————————————————————————–
Explain Call by Value and Call by Reference with appropriate example. OR
Write a program to interchange (swap) value of two variables.
Call By Value
„h In call by value, value of variable is passed during function call.
„h And copy this value in another variable at function definition.
„h In call by value the original value in calling function will never change after execution of function.
Example:
#include
using namespace std;
void swp(int a, int b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x,y;
cout<“enter value=”” of=”” a=”” and=””>”enter>
cin>>x>>y;
swp(x,y);
cout<\nafter swapping=”” a=””>\nafter><><>
return 0;
}
Output:
Enter value of a and b:
4
5
After swapping a = 5 and b = 4
Call By Reference
„h In call by reference, reference is passed during function call.
„h The formal arguments in the called function become aliases to the actual function call.
„h In call by reference the original value in calling function will change after execution of function.
Example:
#include
using namespace std;
void swap(int &a, int &b) // It is the only difference from above program
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int a,b;
cout<“enter two=””>”enter>
cin>>a>>b;
swap(a,b);
cout<><><><>
return 0;
}
Output:
Enter value of a and b:
4
5
After swapping a = 5 and b = 4
————————————————————————————————————————————————————-
Explain member dereferencing operators in short.
C++ provides three pointers to member operators to access the class member through pointer.
::* To declare a pointer to a member of a class.
.* To access a member using object name and a pointer to that member.
->* To access a member using a pointer to the object and a pointer to that member
—————————————————————————————————————————————————————
Explain implicit and explicit type conversion in short.
Implicit Conversion:
– Wheneer data types are mixed in an expression, C++ performs the conversions automatically. This process
is known as implicit or automatic conversion.
Example: m= 5+2.5;
– For a binary operator, if the operands type differs, the compiler converts one of them to match with the
other.
-Using the rule that the smaller type is converted to wider type.
– For example if one operand is integer and another is float then integer is converted to float because float is
wider than integer.
– In above example answer means m, will be in float.
Explicit conversion:
– C++ permits explicit type conversion of variables or expressions using the type cast operator.
– Syntax: type_name (expression)
– Example: average = sum/float(i);
– Alternatively we can use typedef to create an identifier of the required type and use it in the functional
notation.
Example:
typedef int * int_ptr;
p = int_ptr(q);
Example:
#include
using namespace std;
int main()
{
int intvar=5;
float floatvar=3.5;
cout<“intvar ==””>”intvar><>
cout<“\nfloatvar ==””>”\nfloatvar><>
cout<“\nfloat(intvar) ==””>”\nfloat(intvar)><>
cout<“\nint(floatvar) ==””>”\nint(floatvar)><>
}
Output:
intvar = 5
floatvar = 3.5
float(intvar) = 5
int(floatvar) = 3
——————————————————————————————————————————————————————-
Explain operator overloading with example.
– Operator overloading is compile time polymorphism.
– The operator overloading provides mechanism to perform operations on user defined data type.
– We can give special meaning to any operators in which program it is implemented.
– Rules for operator overloading
1. Only existing operator can be overloaded.
2. The overloaded operator must have at least one operand that is user defined type.
3. We cannot change the basic meaning and syntax of an operator.
4. We cannot use friend function to overload certain operators. However member function can be used
to overload them.
5. Unary operators, overloaded by means of a member function, take no explicit argument s and return
no explicit value, but, those overloaded by means of a friend function, take one reference argument.
6. Binary operators overloaded through a member function take one explicit argument and those which
are overloaded through a friend function take two explicit arguments.
7. When using binary operators overloaded through a member function, the left hand operand must be
an object of the relevant class.
8. We cannot overload following operators.
Operator Name
. and .* Class member access operator
:: Scope Resolution Operator
sizeof() Size Operator
?: Conditional Operator
– Example for Unary Operator Overloading
#include
using namespace std;
class sample
{
int a,b;
public:
void getdata()
{
a=10;
b=20
}
void operator -() //Unary Member Function
{
a = a – 5;
b = b – 5;
}
void disp()
{
cout<“\nthe value=”” of=”” a=”< cout<<” \nthe=”” value=”” of=”” b=”< }
};
int main()
{
sample S;
S.getdata();
-S; //Call Unary Member Function
S.disp();
getch();
return 0;
}
————————————————————————————————————————————————————-
“\nthe>