Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assi C++

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

BT 08-01

OBJECT ORIENTED PROGRAMMING

1: - WHAT ARE OBJECT AND CLASSES?


ANS: - The most important feature of c++ are objects and classes.

Class: -

A class is an extension of the idea of structure used in c.


A class is a way to bind and its associated function together. It allows the data and
function to be hidden, if necessary from external use. Generally a class specification has
two parts.

• Class declaration
• Class function definitions.

The class declaration describes the type and scope of its numbers. The class
function definition describes how the class functions are implemented.

The general form of a class is:


Class<class name>
{
private:
variable declaration;
function declaration;
public:
variable declaration;
variable declaration;
};

the body of class is enclosed within braces and terminated by a semicolon.


The class body contain the declaration of variable and function. These collectively called
members. They are usually grouped under two section, namely, private and public and
denote which of the members are private and which of them are public. The member that
have been declared as private can be accessed only from within the class, on the other
hand, public member can be accessed from outside the class also.

Object:

The fundamental idea behind object oriented language is to combine into a


single unit both and the functions that operate on the data such a unit is called an object.
Once a class has been declared, we can create variable of that type by using
the class name.
For example:

Student x;
Create a variable x of type student. In c++ the class variable are known as objects,
therefore, x is called an object of type student, we may also declare more than one object
in one statement such as x, y, z.

Example: -
Class student
{
int roll;
char name[]
public:
void getdata ( )
{
cout<<”enter roll no”;
cin>>name;
}
void showdata( )
{
cout<<”name=”<<name<<endl;
cout<<”roll no=”<<roll;
}
};
void main( )
{
student x;
x. getdata ( )
x. showdata ( )
}

2: - EXPLAIN POLYMORHISM AND OVERLOADING WITH EXMPLE.


ANS: -
Polymorphism: -

The word polymorphism originates from Greek word where poly means
many and mophism means form and thus polymorphism means many form.
Polymorphism is the process of defining a number of object of different classes
into a group and call the method to carry out the operation of objects using different
function calls.
The keyword virtual is used to perform the polymorphism concept in C++.

Overloading : -

It is a logical method of calling several function with different arguments


and data types.
Example:
Swap function is used to the different data such s int, float and char.

• Swapping of two integers


Void swap int (int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

• Swapping of two characters


Void swap_char(char &a, char &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

3: - WHAT IS WHITE SPACE AND ESCAPE SEQUENCE?


ANS: -
White space: -
It may defined as space, carriage returns, line feeds, tabs, vertical tabs and
form feeds. These are ignored by the compilers. But these are few exceptions.

1) The strings constants can not be split.


2) # include<header file> must be written as a single line.
3) // symbol are used to indicate comments. This is valid only till the end of that line.

Escape sequence: -

Escape sequences are denoted by a backslash (\) and a character.


‘\a’ alert a ball character
‘\n’ new line
‘\h’ horizontal tab
‘\b’ backspace
‘\r’ carriage return
‘\f’ form feed
‘\v’ vertical tab
‘\\’ back slash
‘\” single quote
‘\o’ null character
‘\?’ question mark
‘\ooo’ octal value
‘\xhhh’ hexadecimal value
4: - EXPLAIN ENUMERATED DATA TYPES WITH EXAMPLES.
ANS: - The data types with example are mentioned below: -

Integer : -
The data type ’int’ are whole numbers with a range of values supported by
a particular machine. For instance, in a 16-bit word length machine the integer values lie
between -32768 to +32768.

Long integer: -
The value of integer lie in long int is more than that of integer i.e.
-21474836648 to +2147483648.

Floating point vlue: -


Floating point numbers are numbers that have a decimal point. The value
in this data type lie between -3.4e48 to +3.4e48.

Character: -
The keyword ‘char’ defines the character data type char has a range from
-128 to +128.

Type specified Declaration Format specified Storage

Int Integer data type %d or %i 2 bytes

Short int %hd

Long int %id

Unsigned int %u

Float Floating value data type %e 4 bytes

Double %f 8 bytes

Long double %le or %if 10 bytes

char Character data type %c 1 byte


5 : - EXPLAIN STRUCTURE.
ANS : -
We know array is a collection of similar object/data type such as int and float, however, if
we want to represent a collection of data item of different types a single name we can’t
use an array, thus c++ supports a constructed data types known as “structure”, which is a
method for collection data of different types.
Thus a structure is a convenient tool for handling a group of logical related
data items, for example it can be used to represent set of attributes, such s student_name,
roll_number and marks etc.

Structure definition: -
The key word”struct” declares a structure.
Syntax: -
Struct <tag_name>
{
data type member 1:
data type member 2:
-----------
-----------
};
In this stage number memory space is reserved but when
structure variable is declared then memory space is reserved.
Structure variable declaration: -
We can declared structure variable using the tag_name anywhere in the
program.
Example: -
Struct student x;
Or struct student
{
int roll;
char name[20]
--------
--------
} x;
note: - the structure must be terminated with a semicolon.

To access the member of structure: -


The member of a structure are accessed using the dot operators.

Ex: x. roll;
x. name;
INPUT
Scanf (“%d”, & x. roll);
Scanf (“%s”, x. roll);
OUTPUT
Printf(“%d”, x. roll);
Printf(“%d”, x. name);
BT 08-02
OBJECT ORIENTED PROGRAMMING

1: - EXPLAIN ONE WAY, TWO WAYS & MULTI WAYS OF BRNCHING


WITH EXAMPLE.

ANS: - We know very well that “c++” programmings are a collection of functions which are
normally exerted sequentially in the order i.e. statement by statement. However, in
practice we have a number of situation where we may have to change the order of
execution of statement based on certain condition, or repeat a group of statement until
certain specified condition are met. This involves a kind of decision making to see
whether a particular condition has occurred or not and direct the computer and execute
certain statement accordingly.

C ++ provides different type of branching: -


 one way branching
a) if statement
 two way branching
a) if else statement
 multi way branching
a) nested if else statement

ONE WAY BRNCHING: -


One way means evaluating a condition and the branching.
a) if statement: -
The if statement is a powerful decision making statement and
is used to control the flow of execution of statement.
Syntax : - if (expression)
{
statement sequence
}
where: -

if: I is a reserved word


expression: is Boolean expression(condition)
statement sequence: i.e. may be either a statement or a blank of statement. In
simple way, we can say that in if else statement if the result is true then the statement
sequence is executed otherwise the control goes on with the rest of the program.
Example :
# include<iostream.h>
void main( )
{
int x,y;
cout<<”enter the two number:”;
cin>>x>>y;
if(x= = y)
cout<<”number are equal”<<endl;
}

TWO WAY BRANCHING: -


To way branching is used condition or in situation.

a) if else statement: -

syntax: if(expression)
{
statement sequence 1
}
else
{
statement sequence 2
}
where: -

if: - is a reserved word.


Expression: - condition
Statement sequence 1: - can e simple or a compound statement
Else : - is a reversed word
Statement sequence 2: - can be simple or a compound statement. If the “Expression” is
true then the block of statement is executed.

Note: -
In either case, either true or false block will be executed, not both.

Example: -
// program to detect large number
# include<iostream.h>
void main( )
{
int a, b;
cout<<”enter the two random numbers:”;
cin>>a>>b;
if (a>b)
{
cout<<”big numbers=”<<a;
}
else
{
cout<<”big numbers=”<<b;
}
// end of pogram
MULTI WAYS BRACHING: -
The multi way branching can be achieved by a nested if then
else.

a) nested if else statement: -


the statement sequence of it or else may contain another if
statement i.e. the if else statement can be nested with in one another.

Syntax: - if(expression)

if(expression)
{-----------;
----------;
}
else
{-----;
-----;
----}

example: - // program to detected those number from three number.


# include<iostream.h>
void main( )
{
int a, b, c;
cout<<”enter the three number no”;
cin>>a>>b>>c;
if (a>b)
if (a>c)
{
cout<<”big no:”<<a;
}
else if (a>b)
{
cout<<”big no:”<<b;
}
else
{
cout<<”big no:”<<c;
}
} // end of main program
2: - WHAT ARE PUBLIC & PRIVATE CLASSES? ILLUSTRATE WITH EXMPLE.
ANS: - The class body contain the declaration of it member (data and function). A class is a way
to bend the data and its associated function together. It allows the data and function to
be hidden, if necessary, from external use.

There are generally two types of member is a class:


Private and public, the keyword private and public are known as visibility labels. These
keyword are followed by a colon:

The members that have been declared as private can be


accessed only from with in the class. On the other hand, public members can be accessed
from outside the class also. The uses of keyword private as optional, by default the
member of a class are private. If the both labels are missing then by default all members
are private, such a class is completely hidden from the outside world and does not serve
any purpose.

Example of private and public class:

Public class:-

Public member are the member(data & function) that can be used by any function for
example :

Class student
{
public:
int a;
int aqr (int a)
{
return (a*b);
}
};

void main( )
{
student x;
int b;
x.a=10; //ok because public data member can be used any function
b=x. sqr (15); // ok because public member function can be invoiced
------- from any function
-------
-----
}
Private class:
Private member are the class member that are hidden from the outside the
private member of a class can be used only by member function ofsme class in which it is
declared.
For example:
Class dtudent
{
int a;
int sqr (int a)
{
return (a*a);
}
public: int b;
int twice (int i)
{
return (2*i);
}
};

void main( )
{
student x;
x. b=5; // ok b is public member
x.a=5; // wrong because a is a pivate member and hence can be accessed
only by member function and directly by using the object.
x. twice(10); // ok twice is a public member function
x. sqr(10); // wrong because sqr ( ) is a private member function
}

3: - WRITE A PROGRAM TO SORT THE ARRAY OF NUMBER.


ANS: -

// program to sort the array of number


# include <iostream.h>
# include <conio.h>
void main()
{
int num[100], I, j, temp, n;
clrscr( );
cout<<”enter the how many no:”;
cin>>n;

//enter the data


for(i=0;i<n;i++)
{
cout<<”enter the<<i+1<<”number”;
cin>>num[i];
}

// display the data


cout<<”the number are”<<endl;
for ( i=0;i<n;i++)
{
cout<<num[I];
}

// sorting the data


for (i=0;i<n-1;i++)
{
for( j=I+1;i<n;i++)
{
if( num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}

// to display the sorted data


cout<<”number in ascending order “<<endl;
for(i=0;i<n;i++)
cout<<num[i]<<”\t”
getch( );
} //end of the program

4: - EXPLAIN DIFFERENT STORAGE TYPES IN C++.


ANS: -
In c++, a variable can be declared or stored as belonging to any of the following groups

• Automatic variable
• Register variable
• Static variable
• External variable

Normally this is known as “storage class” in c++

• Automatic variable: -
The variable of type automatic are created automatically on
function call and destroyed automatically when function execution is over.
The old compiler it was compulsory to use the keyword ”auto” to
declare automatic variable. but now a days it is taken by default.
The general syntax of automatic variable is,

auto data_type variable_1, variable_2, ………variable_n;

• Register variable: -
The variable declare as “register” type are given space in the CPU’s
register. In register the contents of variable very near to the ALU and hence computation
will be much faster.
The general syntax of register variable is,

register dat_type vriable_1, vriable_2, ……….variable_n ;


E.g. Register int x,y;

• Static variable: -
The variable of this type retains the previous values, the keyword
‘static’ are used to declare static variable.
The general syntax of static variable is,

static data_type, variable_1, vriable_2, ………variable_n;


E.g. static int x=10;

• External variable: -
The variable declared out side a function is an extern variable. The
keyword ‘extern’ are used to declare static variable;
The general syntax of extern variable is,

extern data_type variable_1, variable_2, …….. variable_n;


E.g. extern int x, y;

5: - EXPLAIN THE FEATURES OF OBJECT ORIENTED PROGRAMMING.


ANS: -
Object oriented programming: -
All real world entities are represented as object, in an “oop”
language.
Ex.- student, employ, vehicle
A object is an instance of a class.
Object oriented programming language is a software development technique. It detect the
drawback of procedural language.
Oop is a combination of both data & function on in to single unit or class.
Feature of oop: -
 Data hiding: -
 Data encapsulation: -
 Inheritance: -
 Ploynorphion: -
Data hiding: -
The data member are accessible only to the public of the sme class
and not accessible directly from outside.
We know data is a class these are two types of member private and
public the member that have been declared as private can be accessed only from within
the class. On the other hand public member can be accessed from outside the class also. If
the both labels are missing then by default all the members are private, such a that class
is lately hidden from the outside the world and does not serve any purpose.
Data encapsulation: -
Encapsulation is the most fundamental concept of oop. It is the way
of combining both data and the function under a single unit (class).
The only way to access the data is provided by the function. The
unction are called member function in c++. The data can not be accessed directly. If we
want to read a data in an object then we call a member function of the object.

Inheritance: -
Inheritance is the capabilities of one class of thing inherit
capabilities or properties from another class. i.e. class are divided on to
subclass.
For E.g. the car is a part of the car vehicles which is again a part of
the class vehicles.
In oop, Inheritance gives rise to look reusability, this means we can
add additional feature to existing class without modifying it. This is possible
a new class from the existing one. The new class will have the combined
feature of both the class.
Polymorphism: -
It is the combination of poly + morphism. Polymorphism is the bility
for a message or data to be processed in more than one from.
For E.g.: “human” is a subclass of “mammal”, but “dog” & “cat”
are also subclass of “mammal”, mammal can see day and night, then human cat and dog
to every one is selected but a message select only a mammals whose see day night, if a
message select only a mammals whose seen night, thus only cats selected. Here cat
(mammals) in response to a message or action, is polymorphism.
The polymorphism is a property by which the same message can be
sent to object of several different classes.

You might also like