Object-oriented programming
Object-oriented
programming (OOP) is a programming language model organized around
"objects" rather than "actions" and data rather than logic.
Historically, a program has been viewed as a logical procedure that takes input
data, processes it, and produces output data.
The
programming challenge was seen as how to write the logic, not how to define the
data. Object-oriented programming takes the view that what we really care about
are the objects we want to manipulate rather than the logic required to manipulate
them. Examples of objects range from human beings (described by name, address,
and so forth) to buildings and floors (whose properties can be described and
managed) down to the little widgets on your computer desktop (such as buttons
and scroll bars).
The
first step in OOP is to identify all the objects you want to manipulate and how
they relate to each other, an exercise often known as data modeling. Once
you've identified an object, you generalize it as a class of objects (think of
Plato's concept of the "ideal" chair that stands for all chairs) and
define the kind of data it contains and any logic sequences that can manipulate
it. Each distinct logic sequence is known as a method. A real instance of a
class is called (no surprise here) an "object" or, in some
environments, an "instance of a class." The object or class instance
is what you run in the computer. Its methods provide computer instructions and
the class object characteristics provide relevant data. You communicate with
objects - and they communicate with each other - with well-defined interfaces
called messages.
What are the merits of oops?
· The concept of a data class makes it possible to
define subclasses of data objects that share some or all of the main class characteristics.
Called inheritance, this property of OOP forces a more thorough data analysis,
reduces development time, and ensures more accurate coding.
· Since a class defines only the data it needs to be
concerned with, when an instance of that class (an object) is run, the code
will not be able to accidentally access other program data. This characteristic
of data hiding provides greater system security and avoids unintended data
corruption.
· The definition of a class is reusable not only by
the program for which it is initially created but also by other object-oriented
programs (and, for this reason, can be more easily distributed for use in
networks).
· The concept of data classes allows a programmer to
create any new data type that is not already defined in the language itself.
Simula
was the first object-oriented programming language. Java, Python, C++, Visual
Basic .NET and Ruby are the most popular OOP languages today.
What
is the difference between object oriented programming and procedure oriented
programming?
POP
|
OOP
|
1. In POP, importance is given to the sequence of
things to be done i.e. algorithms.
2. In POP,
larger programs are divided into functions and in OOP, larger programs are
divided into objects.
3. In POP, most functions share global data i.e data
move freely around the system from function to function.
4. In POP,
there is no access specifier.
5. In POP,
Data moves openly around the system from function to function.
6. POP
follows a top down approach in problem solving
|
1.In OOP,
importance is given to the data.
2. In OOP, larger programs are divided into objects.
3. In OOP mostly the data is private and only functions
inside the object can access the data.
4. In OOP there are public, private and protected specifier. 5. In OOP objects communicate with each other through member functions
6. OOP follows a bottom up approach.
|
Write
a program to check whether a given number is even or odd.
#include <stdio.h>
#include <conio.h>
void main() {
int a;
printf("Enter a: \n");
scanf("%d", &a);
if (a % 2 == 0) {
printf("The given
number is EVEN\n");
}
else {
printf("The given
number is ODD\n");
}
getch();
}
Write
a program to find the greatest of three numbers.
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
void main(){
int a, b, c;
printf("Enter a,b,c:
\n");
scanf("%d %d %d",
&a, &b, &c);
if (a > b && a >
c) {
printf("a is Greater than b
and c");
}
else if (b > a && b
> c) {
printf("b is Greater than a
and c");
}
else if (c > a && c
> b) {
printf("c is Greater than a
and b");
}
else {
printf("all are equal or
any two values are equal");
}
getch();
}
Write a program to
check whether a given number is a prime.
A prime number is a natural
number that has only one and itself as factors. Examples: 2, 3, 5, 7,… are
prime numbers. Program:
#include <stdio.h>
#include <conio.h>
void main() {
int n, i, count= 0;
printf("Enter any number n:
\n");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 2) {
printf("n is a Prime
number");
}
else {
printf("n is not a
Prime number");
}
getch();
}
Write a program to check whether a given number
is a palindromic number.
If a number, which when read in
both forward and backward way is same, then such a number is called a
palindrome number.
Program:
#include <stdio.h>
#include <conio.h>
void main() {
int n, n1, rev = 0, rem;
printf("Enter any
number: \n");
scanf("%d",
&n);
n1 = n;
while (n > 0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if (n1 == rev){
printf("Given number
is a palindromic number");
}
else{
printf("Given number
is not a palindromic number");
}
getch();
}
Write a program to find a program to find a
Factorial of a number?
Factorial program in c: c code to find and
print factorial of a number, three methods are given, first one uses a for
loop, second uses a function to find factorial and third using recursion.
Factorial is represented using !, so five factorial will be written as 5!, n
factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero
factorial is defined as one i.e. 0!=1.
Factorial program in c using for loop
#include<stdio.h>
#include<conio.h>
void main()
{
int c, n, fact = 1;
printf("Enter a number to
calculate it's factorial\n");
scanf("%d",&n);
for( c = 1 ; c <= n ; c++ )
fact = fact*c;
printf("Factorial of %d =
%d\n",n,fact);
getch();
}
Factorial program in c using function
#include<stdio.h>
#include<conio.h>
long factorial(int);
void main()
{
int number;
long fact = 1;
printf("Enter a number to
calculate it's factorial\n");
scanf("%d",&number);
printf("%d! = %ld\n",
number, factorial(number));
getch();
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
Factorial program in c using recursion
#include<stdio.h>
#include<conio.h>
long factorial(int);
void main()
{
int num;
long f;
printf("ENTER A NUMBER TO
FIND FACTORIAL :");
scanf("%d",&num);
if(num<0)
printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
else
{
f =
factorial(num);
printf("%d!=%ld",num,f);
}
getch();
}
long factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}
Write a Program to generate Fibonacci sequence?
What is Fibonacci Series?
The first two Fibonacci numbers are 0 and 1 then next number is addition of previous two numbers.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.....
In mathematics it is defined by recurrence relation.
Program Code
#include "stdio.h"
#include "conio.h"
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}
Explain
Constructors and destructors in C++
Constructors:
What is the use of Constructor?
The main use of
constructors is to initialize objects. The function of initialization is
automatically carried out by the use of a special member function called a
constructor.
General Syntax of Constructor
A constructor is a
special member function that takes the same name as the class name. The syntax
generally is as given below:
The default
constructor for a class X has the form
X::X()
In the above example,
the arguments are optional.
The constructor is
automatically named when an object is created. A constructor is named whenever
an object is defined or dynamically allocated using the "new"
operator.
There are several
forms in which a constructor can take its shape namely:
Default Constructor:
This constructor has
no arguments in it. The default Constructor is also called as the no argument
constructor.
For
example:
Sample Code
1. class Exforsys
2. {
3. private:
4.
int a,b;
5. public:
6.
Exforsys();
7.
...
8. };
9.
1. Exforsys :: Exforsys()
1. {
1. a=0;
1. b=0;
1. }
Copyright exforsys.com
Copy constructor:
This constructor takes
one argument, also called one argument constructor. The main use of copy
constructor is to initialize the objects while in creation, also used to copy
an object. The copy constructor allows the programmer to create a new object
from an existing one by initialization.
For example to invoke
a copy constructor the programmer writes:
Exforsys e3(e2);
or
Exforsys e3=e2;
Both the above formats
can be sued to invoke a copy constructor.
For
Example:
Sample Code
1. #include
<iostream>
2. using namespace std;
3. class Exforsys
4. {
5. private:
6.
int a;
7. public:
8.
Exforsys()
9.
{ }
1.
Exforsys(int w)
1. {
1.
a=w;
1. }
1.
Exforsys(Exforsys& e)
1. {
1.
a=e.a;
1.
cout << " Example of Copy Constructor";
1. }
1. void result()
2. {
2.
cout<< a;
2. }
2. };
2. void main()
2 {
2.
Exforsys e1(50);
2.
Exforsys e3(e1);
2.
cout<< "ne3=";e3.result();
2. }
Copyright exforsys.com
In the above the copy
constructor takes one argument an object of type Exforsys which is passed by
reference. The output of the above program is
Some important points
about constructors:
§ * A constructor takes the same name as the
class name.
* The programmer cannot declare a constructor
as virtual or static, nor can the programmer declare a constructor as const,
volatile, or const volatile.
* No return type is specified for a
constructor.
* The constructor must be defined in the
public. The constructor must be a public member.
* Overloading of constructors is possible.
This will be explained in later sections of this tutorial.
Destructors
What
is the use of Destructors
Destructors are also
special member functions used in C++ programming language. Destructors have the
opposite function of a constructor. The main use of destructors is to release
dynamic allocated memory. Destructors are used to free memory, release
resources and to perform other clean up. Destructors are automatically named
when an object is destroyed. Like constructors, destructors also take the same
name as that of the class name.
General
Syntax of Destructors
~ classname();
The above is the
general syntax of a destructor. In the above, the symbol tilda ~ represents a
destructor which precedes the name of the class.
Some important points
about destructors:
§ Destructors take the same name as the class
name.
§ Like the constructor, the destructor must
also be defined in the public. The destructor must be a
public member.
§ The Destructor does not take any argument
which means that destructors cannot be overloaded.
§ No return type is specified for destructors.
For
example:
Sample Code
1. class Exforsys
2. {
3.
private:
4.
...
5. public:
6.
Exforsys()
7.
{ }
8.
~ Exforsys()
9.
{ }
1. }
Array
Array by definition is a variable that hold multiple elements
which has the same data type.
Declaring
Arrays
We can declare an array by specifying its data type, name and
the fixed number of elements the array holds between square brackets
immediately following the array name.
The following illustrates how to declare an array:
1 |
data_type
array_name[size]; |
For example, to declare an array that contains 100 integer
numbers you can do as follows:
1 |
int a[100]; |
You must follow the rules below when you declare an array in C:
§
The data type can be any valid C data types including C structure and
union.
§
The name of an array has to
follow the rule of C variables.
§
The size of array has to be a positive
constant integer.
Initializing
Arrays
It is like a variable, an array can be initialized. To
initialize an array, you provide initializing values which are enclosed within
curly braces in the declaration and placed following an equals sign after the
array name.
Here is an example of initializing an array of integers.
1 |
int list[5] =
{2,1,3,7,8}; |
Accessing
Array Element
You can access array elements via indexes like array_name[index].
Indexes of array starts from 0 not 1 so the highest elements of
an array is array_name[size-1].
Multidimensional
Arrays
An array with more than one index value is called a
multidimensional array. All the arrays you used above are called
single-dimensional arrays.
To declare a multidimensional array you can do as follows:
1 |
data_type
array_name[][][]; |
The number of square brackets specifies the dimension of the
array. For example to declare two dimensions integer array we can do as
follows:
1 |
int matrix[3][3]; |
Initializing
Multidimensional Arrays
You can initialize an array as a single-dimension array. Here is
an example of initializing an two dimensions integer array:
1 |
int matrix[3][3] = |
|
2 |
{ |
3 |
{11,12,13}, |
4 |
{21,22,23}, |
5 |
{32,31,33}, | |
Program to find the sum and average to 100 items using array.
#include <iostream>
#include <conio.h>
void main ()
{
int num[100];
int n;
register int i;
int sum = 0;
float avg;
cout << "Enter number of items in array: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter the " << i+1 << "th number: ";
cin >> num[i];
sum += num[i];
}
avg = (float) sum/n;
cout << "Sum is " << sum << "\nAnd Average = " << avg;
getch();
#include <conio.h>
void main ()
{
int num[100];
int n;
register int i;
int sum = 0;
float avg;
cout << "Enter number of items in array: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter the " << i+1 << "th number: ";
cin >> num[i];
sum += num[i];
}
avg = (float) sum/n;
cout << "Sum is " << sum << "\nAnd Average = " << avg;
getch();
}
Structure
Defining structure
Declaring structure
Complex structure
Accessing structure member
Structure
In some programming contexts, you need to access multiple data
types under a single name for easier data manipulation; for example you want to
refer to address with multiple data like house number, street, zip code,
country. C supports structure which allows you to wrap one or more variables
with different data types. A structure can contain any valid data types like
int, char, float even arrays or even other structures. Each variable in
structure is called a structure member.
Defining structure
To define a structure, you use struct keyword. Here is the common
syntax of structure definition:
struct struct_name{ structure_member };
The name of structure follows the rule of variable name. Here is
an example of defining address structure:
1 |
struct address{ |
|
2 |
unsigned int house_number; |
3 |
char street_name[50]; |
|
4 |
int zip_code; |
5 |
char country[50]; |
|
6 |
}; |
The address structure contains house number as an
positive integer, street name as a string, zip code as an integer and country
as a string.
Declaring structure
The above example only defines an address structure without creating any
structure instance. To create or declare a structure instance, you can do it in
two ways:
The first way is to declare a structure followed by structure
definition like this :
1 |
struct struct_name
{ |
|
2 |
structure_member; |
3 |
... |
|
4 |
}
instance_1,instance_2 instance_n; |
In the second way, you can declare the structure instance at a
different location in your source code after structure definition. Here is
structure declaration syntax :
1 |
struct struct_name
instance_1,instance_2 instance_n; |
Complex structure
If a structure contains arrays or other structures, it is called
complex structure. For example address structure is a structure. We can
define a complex structure calledcustomer which contains address structure as follows:
1 |
struct customer{ |
|
2 |
char name[50]; |
3 |
structure
address billing_addr; |
|
4 |
structure
address shipping_addr; |
5 |
}; |
Accessing structure member
To access structure members we can use dot operator (.) between
structure name and structure member name as follows:
structure_name.structure_member
For example to access street name of structure address we do as follows:
1 |
struct address
billing_addr; |
|
2 |
billing_addr.country
= "US"; |
If the structure contains another structure, we can use dot
operator to access nested structure and use dot operator again to access
variables of nested structure.
1 |
struct customer jack; |
|
2 |
jack.billing_addr.country
= "US"; |
nice blog.....
ReplyDelete