Skip to content

Instantly share code, notes, and snippets.

@sohanmanju
Last active May 29, 2019 14:00
Show Gist options
  • Save sohanmanju/ba98bfea8b20a302a87c3ec8fd97bb73 to your computer and use it in GitHub Desktop.
Save sohanmanju/ba98bfea8b20a302a87c3ec8fd97bb73 to your computer and use it in GitHub Desktop.
OOPS 1
1.Program to perform swap of two int and float using function overloading.
#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
void swap(float &,float &);
main()
{
clrscr();
int int_1;
int int_2;
float float_1;
float float_2;
cout<<"\n ********* Data Type - int **********"<<endl;
cout<<"\n Enter the value of int_1 = ";
cin>>int_1;
cout<<"\n Enter the value of int_2 = ";
cin>>int_2;
cout<<"\n ********* Data Type - floatt **********"<<endl;
cout<<"\n Enter the value of floatt_1 = ";
cin>>float_1;
cout<<"\n Enter the value of float_2 = ";
cin>>float_2;
cout<<"\n ********* Before Swap() **********"<<endl;
cout<<"\n Values of int :"<<endl;
cout<<"\t\t Value of int_1 = "<<int_1<<endl;
cout<<"\t\t Value of int_2 = "<<int_2<<endl;
cout<<" Values of float :"<<endl;
cout<<"\t\t Value of float_1 = "<<float_1<<endl;
cout<<"\t\t Value of float_2 = "<<float_2<<endl;
cout<<"\n ********* After Swap() **********"<<endl;
cout<<"\n Values of int :"<<endl;
swap(int_1,int_2);
cout<<"\t\t Value of int_1 = "<<int_1<<endl;
cout<<"\t\t Value of int_2 = "<<int_2<<endl;
cout<<" Values of float :"<<endl;
swap(float_1,float_2);
cout<<"\t\t Value of float_1 = "<<float_1<<endl;
cout<<"\t\t Value of float_2 = "<<float_2<<endl;
getch();
return 0;
}
void swap(int &value_1,int &value_2)
{
int temp;
temp=value_2;
value_2=value_1;
value_1=temp;
}
void swap(float &value_1,float &value_2)
{
float temp;
temp=value_2;
value_2=value_1;
value_1=temp;
}
2.Function Componenents:
-Function Prototype/Declaration
Function declaration informs the compiler about the function's name, type and number of argument it receives and type of value it returns.
Syntax for function declaration
returntype function_name ([arguments type]);
-Function Body/Definition:
It is the most important part of function which that consists of body of function. It consists of block of statements that specifies what task is to be performed. When a function is called, the control is transferred to the function definition.
Syntax for function definition
returntype function_name ([arguments])
{
statement(s);
... ... ...
}
-Function Call:
Function call statement calls the function by matching its name and arguments. A function call can be made by using function name and providing the required parameters.
Syntax for function call
function_name ([actual arguments]);
3. Inline Functions are faster than regular function, Explain and give example.
The C++ inline function provides an alternative. With inline code, the compiler replaces the function call statement with the function code itself (this process is called expansion) and then compiles the entire code. Thus, with this the compiler doesn't have to do the long, time consuming process.
Main Advantage of Inline Functions : They save on overheads of a function call as it's not invoked, rather its code is replaced in the program.
Main Disadvantage of Inline Functions : With more function calls (for ex: in a loop), the repeated occurrences of same function code wastes memory space.
It's faster than actual function because everytime an actual function is called the complier has to go to the the address of the actual function when called and then fetching data members from elsewhere, All this is a tedious process which takes up more time and memory.
Example:
#include <iostream>
using namespace std;
inline int Max(int x, int y) {
return (x > y)? x : y;
}
// Main function for the program
int main() {
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
4. Program to Illustrate Static Data member.
#include<iostream.h>
#include<conio.h>
class stat {
int code;
static int count;
public:
stat() {
code = ++count;
}
void showcode() {
cout << "\n\tObject number is :" << code;
}
static void showcount() {
cout << "\n\tCount Objects :" << count;
}
};
int stat::count;
void main() {
clrscr();
stat obj1, obj2;
obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
getch();
}
5.Data Hiding in C++
Data hiding is a software development technique specifically used in object-oriented programming (OOP) to hide internal object details (data members). Data hiding ensures exclusive data access to class members and protects object integrity by preventing unintended or intended changes.
Data hiding also reduces system complexity for increased robustness by limiting interdependencies between software components.
Data hiding is also known as data encapsulation or information hiding.
Data hiding was introduced as part of the OOP methodology, in which a program is segregated into objects with specific data and functions. This technique enhances a programmer’s ability to create classes with unique data sets and functions, avoiding unnecessary penetration from other program classes.
Because software architecture techniques rarely differ, there are few data hiding contradictions. Data hiding only hides class data components, whereas data encapsulation hides class data parts and private methods.
6. Define OOP and it's characteristics.
Object oriented programming – As the name suggests uses objects in programming. Object oriented programming aims to implement real world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operates on them so that no other part of code can access this data except that function.
Different characteristics of an Object Oriented Programming language:
Object: Objects are basic run-time entities in an object oriented system, objects are instances of a class these are defined user defined data types.
Class: Class is a blueprint of data and functions or methods. Class does not take any space.By default class variables are private but in case of structure it is public. in above example person is a class.
Encapsulation and Data abstraction: Wrapping up(combing) of data and functions into a single unit is known as encapsulation. The data is not accessible to the outside world and only those functions which are wrapping in the class can access it. This insulation of the data from direct access by the program is called data hiding or information hiding.
Inheritance: inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. Inheritance provides re usability. This means that we can add additional features to an existing class without modifying it.
Polymorphism: polymorphism means ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation.
C++ supports operator overloading and function overloading.
The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
Dynamic Binding: In dynamic binding, the code to be executed in response to function call is decided at runtime. C++ has virtual functions to support this.
Message Passing: Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent.
7. Bubble Sort using Pointers:
Visit this link: https://stackoverflow.com/questions/16264823/how-to-perform-bubble-sort-using-pointers
Bubble Sort ( Generic Sorting Program)
#include <iostream>
using namespace std;
template <class X> void bubble(X *items,int count)
{
X t;
for(int a=1; a<count; a++)
for(int b=count-1; b>=a; b--)
if(items[b-1] > items[b]) {
t = items[b-1];
items[b-1] = items[b];
items[b] = t;
}
}
int main()
{
int iarray[7] = {7, 5, 4, 3, 9, 8, 6};
bubble(iarray, 7);
cout << "Here is sorted integer array: ";
for(int i=0; i<7; i++)
cout << iarray[i] << ' ';
cout << endl;
return 0;
}
8.Operators in C++:
For Examples ( https://www.tutorialspoint.com/cplusplus/cpp_operators.htm )
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provide the following types of operators −
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
9. Explain the following:
- Recursive Function: When function is called within the same function, it is known as recursion in C++. The function which calls the same function, is known as recursive function.
A function that calls itself, and doesn't perform any task after function call, is known as tail recursion. In tail recursion, we generally call the same function with return statement.
Let's see a simple example of recursion.
recursionfunction(){
recursionfunction(); //calling self function
}
-Scope Resolution Operator: Scope resolution operator (::) in C++ programming language is used to define a function outside a class or when we want to use a global variable but also has a local variable with the same name.
C++ programming code
#include <iostream>
using namespace std;
char c = 'a'; // global variable
int main() {
char c = 'b'; //local variable
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n"; //using scope resolution operator
return 0;
}
Function Overloading: Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++.
Following is a simple C++ example to demonstrate function overloading.
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
10. Argument Passing Mechanism in C++: https://www.includehelp.com/cpp-tutorial/argument-passing-with-its-types.aspx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment