Assi C++
Assi C++
Assi C++
Class: -
• 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.
Object:
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 ( )
}
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 : -
Escape sequence: -
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.
Character: -
The keyword ‘char’ defines the character data type char has a range from
-128 to +128.
Unsigned int %u
Double %f 8 bytes
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.
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
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.
a) if else statement: -
syntax: if(expression)
{
statement sequence 1
}
else
{
statement sequence 2
}
where: -
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.
Syntax: - if(expression)
if(expression)
{-----------;
----------;
}
else
{-----;
-----;
----}
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
}
• Automatic variable
• Register variable
• Static variable
• External variable
• 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,
• 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,
• 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,
• 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,
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.