Monday 9 April 2018

Conditional Operator ( if-else Statement)

Condition: if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.

   If Statement

 

Syntax :

   If(condition)

{

//Statement     OR     code to be executed

}

 

Example :

 

Public class ifExample

{

Public static void main(String[] args)

{

Int age =20;

If(age>18)

{

System.out.println(“Age is greater than 18”);

}

}

}

 

2.    If-else Statement

 

Syntax :

If(condition)

{

//code if condition is true

}

Else

{

// code if condition is false

}

 

               Example :

Public classs ifElseExample

{

Public static void main(String [] args)

{

Int number = 13;

If(number%2==0)

{

System.out.println(“even number”);

}

Else

{

System.out.println(“odd number”);

}

}

}

 Leap Year

A year is leap, if it is divisible by 4 and 400. But, not by 100.

Public class leapyear

{

Public static void main(String[] args)

{

Int year =2020;

If(year%4==0) && (year%100!=0) || (year% 400==0)

{

System.out.println(“LEAP YEAR”);

}

Else

{

System.out.println(“COMMON YEAR”);

}

}

}

 

3.    If-else-if   : The if-else-if ladder statement executes one condition from multiple statements.

Syntax :

If(condition)

{

//code to be executed if condition1 is true

}

Else if(condition)

{

//code to be executed if condition2 is true

}

Else

{

//code to executed if all condition are false

}

Example :

Public class ifelseifexample{

Public static void main(String[] args){

Int marks =65;

If(marks<50){

System.out.println(“Fail”);

}

Elseif(marks>=60 && marks<70){

System.out.println(“D grade”)

}

Elseif(marks>=70 && marks<80){

System.out.println(“B grade”);

} Elseif(marks>=80 && marks<90){

System.out.println(“c grade”);

}else{

System.out.println(“Invaild”);

}

}

}

 

WRP to check positive, negative and zero?

 

Public class positivenegative

{

Public static void main(String[] args)

{

Int number = -15;

If(number>0)

{

System.out.println(“POSITIVE”);

}

Elseif(number<0)

{

System.out.println(“NEGATIVE”);

}

Else{

System.out.println(“ZERO”);

}

}

}

 

4.    Nested if Statement

Syntax :

If(condition){

//code to executed

}

If(condition){

//code to be excuted

}

Example :

 

Publiuc class nestedif{

Public static void main(Strings[] args)

{

Int age=20;

Int weight=80;

If(age>=18){

If(weight>50)

{

System.out.println(“Your are eligible to donate blood”);

}

}

}

}

Assignment operator (Post increment and Pre increment)

Post increment

int x=10;
x=x+1;
System.out.println(“x”);
 ————————————->>>>Output     :-11

int x=10;
x++;                          //increment by one.
System.out.println(“x”);
—————————————->>>>Output   :-11

int x=10;
System.out.println(“x++”);     //Here,first print the x value then, increment by one.
—————————————–>>>> Output :-10

  • Pre increment
int x=10;
System.out.println(“++x”);   //Here,first increment the value of  x then print x.
———————————>>>Output :-  11

int x=10;
++x;                          //First increment by one the Print the value.
System.out.println(“x”);
—————————————->>>>Output   :-  11

Finally, In this slide we show a assignment operator.

Take a input by user

Take the input from user

Syntax:-
Scanner scan=new Scanner(System.in);

Example:-

*/ Package lesson1;
import java until.scanner;
public class Myclass
{
public static void main(string[] arg);
{
Scanner scan=new Scanner(System.in);
System.out.println(“Enter the some number”);
int user-input-number=scan.nextInt();
System.out.println(“The enter value is-“);
System.out.println(“user-input-number”);
}
}
*/

Math arithmetic operator


Here, the math operator is:-
 
  " + "   (Addition)
  " – "   (Subtraction)

  " * "  (Multiplication)
  " / " (Division)

  " %"  (Modulus)

Example:-
public class math;
{
public static void main(string[]  arg)
{
int x,y,answer;
x=60;
y=30;
answer=x+y;   //(perform add operation)
answer=x-y;    //(perform sub operation)
answer=x*y;   //(perform mul operation)
answer=x/y;    //(perform div operation)
answer=x%y;  //(perform mod operation)

System.out.println(“Answer of addition =”+ answer);
}
}

Variable Declare in Java


  • Declare datatype.
  1. byte (number,1 bytes)
  2. short (number,2 bytes)
  3. int (number,4 bytes)
  4. long (number,8 bytes)
  5. float(float number,4 bytes)
  6. double (float number,8 bytes)
  7. char (a character , 2 bytes)
  8. Boolean (true or false , 1 bytes)
  9. Declare (Example)
  10. short my-variable=10;
  11. float my-decimal=(float) 4.5;
  12. double my-double=11.56;
  13. char my-char=’A’;

Introduction of Java

  • Java is develop by James Gosling in 1991
  • Java is a developed in the early 90s in Sun Microsystems company. Java first version release in 1995 (Java 1.0).
  • Java first name is Oak.
  • It is a high -level language.
  • Java is object oriented programing.
  • Java is run on-Java virtual Machine.

    How to run a Java program?

Java Program is run on Jdk (Java develop Kit) Software.
First of all we will install a jdk Software,then next install a eclipse Software.
Java is run on jvm (Java virtual machine).
In below figure we  will show a how to Execute a one simple program. 

Java is a simple and secure language.



Next slide we will explain , how to write a simple java program.

How to use a simple java program


  • In java programing, first create Package .
       package lesson1;                                              //create package
       public class MyClass;                                    // Class is a user-defined function,and class is a key word.
       {
       public static void main(string[] ,arg)             //main function is starting of program.
         {
       System.out.println(“Hello Friends”);              //This function is print the output on output screen.
       }
       }

Pointer and Function

The pointers are very much used in a function. Sometimes only with a pointer a complex function can be easily represented and success. The usage of the pointers in a function defintion may be classified into two groups.
1. Call by Value.
2. Call by reference

Call by Value 
  We have seen that a function in invoked there will be a link established between the formal and actual parameters. A temporary storage is created where the value of actual parameters is stored.

The formal parameters picks up its value from storage area the mechanism of data transfer between actual parameters mechanism of data transfer is referred as call by value.

The corresponding formal parameters represents a local variable in the called function.


This will not change the value of actual parameters.

#include<stdio.h>
void main( )
{
int x,y;
x=20;
y=30;
printf("\n Value of a and b before function call = %d%d",a,b);
fucn(x,y);
printf("\n Value of a and b after function call = %d%d",a,b);
}
fucn(p,q)
int p,q;
{
p=p+q;
q=q+q;
}

Call by Reference
 When we pass address to a function the parameters  receiving the address  should be pointers. The process of calling a function by using pointers to pass the address of the variable is known as call by reference . The function which is called by reference can change the value of the variable used in the call.

/* Example of call by reference */

#include<stdio.h>
void main( )

int x, y;
x=20;
y=30;
printf("\n Value of a and b before function call = %d%d",a,b);
fucn(&x,&y);
printf("\n Value of a and b after function call = %d%d", a,b);
}
fucn(p,q)
int p,q;
{
*p=*p + *p;
*q=*q + *q;
}

Function

A function is a self contained block of statement that performs a coherent task of some kind.

Functions provides modularity to the software.

#include<stdio.h>
int add (int x, int y)
int z;
z = x + y;
return (z);
}
main( )
{
int i,j,k;
i = 10;
j = 20;
k = add(i,j)        /* function call */
printf("The value of k is %d\n",k);
}

Output:-
  The value of k is 30.
 

Pointer

In C language a pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.

Pointer Declaration :
  A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk (*) tells the compiler that you are type * variable name

Example :
 int *ptr;
float *string;

Address Operator :
  Once we declare a pointer variable we must point it to something we can do this by assigning to the pointer the address the address of the variable you want to point as in the following example :
ptr=&num;
   This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.

Memory Space of int Pointer and long double pointer
  
Size of any type of pointer is independent of the data type which it is pointing i.e. size of pointer is always fixed. Size of any type (near) of pointer in C is two byte.

For example :
 #include<stdio.h>
 void main( )
{
int *p1;
long double *p2;
printf("%d %d", sizeof (p1) , sizeof (p2) );
}

Output :   2     2

Since both pointers int and long double are pointing to only first byte of int data and long double data respectively.
   
Hence both int pointer and long double pointer stores only address in 16 bits. Thus both of the them will occupy exactly equal memory spaces.

The size of void Pointer in C :
     
Size of any type of pointer in C is independent of data type which is pointer pointing i.e. size of all type pointer (near) in C is two byte either it is char pointer double pointer, function or null pointer. Void pointer is not exception of this rule and size of void pointer is also two byte.

String Operations (string.h)

C language recognizes that string is a different class of array by letting us input and output the array as a unit and are terminated by null character. C library supports a large number of string handling functions that can be used to carry out many of the string manipulations such as :-

 * Length (Number of character in the string).
 * Concatenation (Adding two or more strings).
 * Comparing two strings.
 * Substring (Extract substring from a given string).
 * Copy (Copies one string over another).

To do all the operation described here it is essential to include string.h library header file in the program.

strlen( ) Function
  This function counts and returns the number of character in a string. The length does not include a null character.

 Syntax
 n=strlen(string);
                     Where n is integer variable which receives the value of length of the string.

Example :
  length = strlen("Irawen");
The function will assign number of character 9 in the string to a integer variable length.


\*Write a C program to find the length of the string using strlen( ) function. */

#include<stdio.h>
#include<string.h>
void main( )
{
char name[100];
int length;
printf("Enter the string");
gets(name);
length = strlen(name);
printf("\n Number of character in the string is=%d",length);
}

strcat( ) Function
 When you combine two strings , you add the character of one string to the end of other string. The process is called concatenation. The strcat( ) function joins two strings together. It takes the following form

Syntax :
  strcat(string1, string2)
   String1 and string2 are character arrays. When the function strcat is executed string2 is appended to string1. The string2 remains unchanged.

Example :
 strcpy(string1, "sri");
 strcpy(string2,"Bhagavan");
 printf("%s",strcat(string1,string2);
  
From the above program segment the value  of string1 becomes sribhagavan. The string at str2 remains unchanged as bhagavan.

strcmp( ) Function
  In C language we cannot directly compare the value of 2 strings in a condition like if(string1==string2).
  
Most libraries however contain the strcmp( ) function, which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp( ) is given below :
       Strcmp(string1,string2)

 String1 and string2 may be string variable or string constants. String1 and string2 may be string variable or string constant some computers return a negative if the string1 is alphabetically less than the second and a positive number if the string is greater than the second.

Example :
strcmp("Newyork","Newyork")  will return zero because 2 strings are equal.

strcmp("their","there") will return a 9 which is the numeric difference between ASCII 'i' and ASCII 'r'.

strcmp("The","the") will return 32 which is the numeric difference between ASCII "T" & "t".


strcmpi( ) Function 
  This function is same as strcmp( ) which compares 2 string but not case sensitive.

Example :
  strcmpi("THE","the");  will return 0;

strcpy( ) Function
  C language not allow you to assign the characters to a string directly as in the statement name="Robert";

Instead use the strcpy( ) function found in most compilers the syntax of the function is illustrated below.
 strcpy(string1,string2);

Strcpy function assign the contents of string2 to string1. String2 may be a character array variable or a string constant.

strcpy(Name,"Robert");
In the above example Robert is assigned to the string called name.

strlwr( ) Funtion
 This function converts all characters in a string from uppercase to lower case.

Syntax:
 strlwr(string);

For example :
strlwr("IRAWEN") converts to Irawen.


strrev( ) Function
  This function reverses the character in a string.

Syntax
  strrev(string);

For example :
strrev("program") reverses the character in a string into "margrop".

strupr( ) Function
  This function converts all character in a string from lower case to uppercase.

Syntax
 strupr(string);

For example :
 strupr("pirawen") will convert the string to PIRAWEN.

/* Example program to use string functions */

#include<stdio.h>
#include<string.h>
void main( )
{
char s1[20], s2[20], s3[20];
int x;
printf("Enter the strings");
scanf("%s%s",s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{
printf("\nStrings are not equal\n");
strcat(s1,s2);
}
else
printf("\nStrings are equal");
strcpy(s3,s1);
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
printf("\n s1=%s\t length=%d characters\n",s1,l1);
printf("\n s2=%s\t length=%d characters\n",s2,l2);
printf("\n s3=%s\t length=%d characters\n",s3,l3);
}

Strings

A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string. In C it is required to do some meaningful operations on strings they are :
* Reading string displaying strings.
* Combining or concatenating strings.
* Comparing string and checking whether they are equal.
* Extraction of a portion of a string.

Strings are stored in memory as ASCII codes of characters that make up the string appended with '\0' (ASCII value of null). Normally each character is stored in one byte, successive character are stored in successive bytes.


The last character is the null character having ASCII value zero.

Initializing Strings

  Following the discussion on character arrays, the initialization of a string must the following form which is simpler to one dimension array.
  char month1[ ] = { 'j' , 'a' , 'n' , 'u' , 'a' , 'r' , 'y'};
Then the string month is initializing to January. This is perfectly valid but C offers a special way to initialize strings. The above string can be initialized char month1 [ ]="January"; . The characters of the string are enclosed within a part of double quotes. The compiler takes care of string enclosed within a pair of a double quotes. The compiler takes care of storing the ASCII codes of characters of the string in the memory and also stores the null terminator in the end.

/* String.c string variable */
#include<stdio.h>
main( )
{
char month[15];
printf("Enter the string");
gets(month);
printf("The string entered is %s",month);
}

In this example string is stored in the character variable month. The string is displayed in the statement.
printf("The string entered is %s",month);

It is one dimension array. Each character occupies a byte. A null character(\0) that has the ASCII value 0 terminates the string. The table shows the storage of string January in the memory recall that \0 specifies a single character whose ASCII  value is zero.



Character string terminated by a null character '\0'.

A string variable is any valid C variable name and is always declared as an array. The general form of declaration of a string variable is
  Char string_name[size];
The size determines the number of characters in the string name.


Example :
  char month[10];
  char address[100];
The size of the array should be one byte more than the actual space occupied by the string since the compiler appends a null character at the end of the string.

Reading String from the Terminal
  The function scanf with %s format specification is needed to read the character string from the terminal.

Example :
 Char address[15];
 scanf("%s",address);

Scanf statement has a drawback it just terminates the statement as soon as it finds a blank space , suppose if we type the string new york then only the string "new" will be read and since there is a blank space after word "new" it will terminate the string.

Note that we can use the scanf( ) without the ampersand symbol before the variable name. In many applications it is required to process text by reading an entire line of next from the terminal.

The function getchar( ) can be used repeatedly to read a sequence of successive single characters and store it in the array.

We cannot manipulate strings since C does not provide any operators for string. For instance we cannot assign one string to another directly.

For example :
 String="xyz";
String1=string2;
Are not valid. To copy the chars in one string to another string we may do so on a character to character basis.


Writing String to Screen
  The printf statement along with format specifier %s to print strings on to the screen. The format %s can be used to display an array of characters that is terminated by the null character. For example : printf("%s",name); can be used to display the entire contents of the array name.

Arithmetic Operations on Characters
 We can also manipulate the characters as we manipulate numbers in C language. Whenever the system encounters the character data it is automatically converted into an integer value by the system. We can represent a character as an interface by using the following method.
                 X='a';
               printf("%d\n",x);
Arithmetic operations can also be performed on characters. For example x='z'-1; is a valid statement. The ASCII value of 'z' is 122 the statement therefore will assign 121 to variable x.

It is also possible to use character constant in relational expressions. For example : ch>'a' && ch <='z' will check whether the character stored in variable ch is a lower case letter.

Abstract Class

Abstract class is one that which not used to create object. An abstract class is designed only to act as base class and it is inherited by other class.

It is designed concept in the program development and provide a base upon which other classes may be built.

Remember that a class containing pure virtual function can not be used to declare the any objects of it own. As stated earlier such classes are called as abstract base class.

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( ) {   }
virtual void putdata( )=0;
};
class B : public A
{
public:
void getdata( )
{
cout<<"\n\t This is B class getdata( )";
}
void putdata( )
{
cout<<"\n\t This is B class putdata( )";
}
};
void main( )
{
clrscr( );
A a;
B b;
b.getdata( );
b.putdata( );
getch( );
}
 

Sunday 8 April 2018

Pure Virtual Function

It is normal practice to declare a function virtual inside the base class and redefine it in the derived classes. The function inside the base class is rarely used for performing any particular task.

It only serves as a place holder. A pure virtual function is a type of the function which has only one function declaration. It does not have the function definition. 

A pure virtual function can be declared in two different ways.
 1. A virtual function does not contain a body
      Ex virtual void getdata( )
{

}
 2. A virtual function may be equated to zero in the function declaration itself
    Ex virtual void getdata( ) = 0;

Above function are called pure virtual function. A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases , the compiler requires each derived class to either define the function or re-declare it is a pure virtual function.

Those function which are only declared but not defined in the base class are called pure virtual function.

Program to demonstrate the pure virtual function

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
virtual void putdata( )=0;
};
class B : public A
{
public:
void getdata( )
{
cout<<"\n\t This is B class getdata( )";
}
void putdata( )
{
cout<<"\n\t This is B class putdata( )";
}
};
void main( )
{
clrscr( );
A *ptr;
B b;
b.getdata( );
b.putdata( );
ptr=&b;
ptr->getdata( );
ptr->putdata( );
getch( );
}

Output:-
This is B class getdata( )
This is B class putdata( )
This is B class getdata( )
This is B class putdata( )

Containership : Classes within classes

As we know that , inheritance is the mechanism of the deriving certain or all properties of one class (base class) into another class (derived class).

Cpp also supports another way of inheriting properties of one class into another class. This approach takes a view that an object is a collection of many other objects. That is a class can contain object objects of another classes as its  members.

A class which contains the object of another class is called container class and this kind of relationship is called containership.

#include<iostream.h>
#include<conio.h>
class date
{
int dd,mm,yyyy;
public:
date( )
{
dd=0 ; mm=0 ; yyyy=0;
}
void read_date(void)
{
cout<<"(dd mm yyyy) :";
cin>>dd>>mm>>yyyy;
}
void display_date(void)
{
cout<<dd<<"\\"<<mm<<"\\"<<yyyy;
}
};
class student
{
int roll_no;
char name[20];
date dob;    //Creating object of data class
public:
void read_student(void)
{
cout<<"\nEnter roll number :";
cin>>roll_no;
cout<<"Enter name :";
cin>>name;
cout<<"Enter date of birth";
dob.read_date( );       // Calling date class function
}
void display(void)
{
cout<<"\nRoll Number :"<<roll_no;
cout<<"\nName :"<<name;
cout<<"\nDate of birth :";
dob.display_date( );
}
};
main( )
{
clrscr( );
student s;
s.read_student( );
s.display( );
getch( );
return(0);
}

Output:-
Enter roll number :101
Enter name : Irawen
Enter date of birth (dd mm yyyy) : 27 10 81
Roll Number : 101
Name : Irawen
Date of birth : 27\10\81

Virtual Destructor

A problem can occur while using polymorphism to process dynamically located object of a class hierarchy. If an object is destroyed explicitly by applying 'delete' operator to a base class pointer to the object, only the base class destructor function is called on the object.

But we are interested to delete whole memory allocated to the base class as well as derived classes. There is simple solution to this problem , declare the virtual base class destructor. This automatically makes all derived class destructor virtual even though they do not have the same name as the base class destructor.

Program to demonstrate the need of virtual destructor

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}

Output:-
This is class B and getdata( ) method
This is a class A destuctor

Program to demonstrate the use of virtual destructor

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
virtual ~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}

Output:-
This is class B and getdata( ) method
This is a class B destructor
This is a class A destructor

Rules of virtual Function

1. The virtual function must be member of some class.

2. They can not be static members because virtual members is always a member of particular object in a class rather than member of class as a whole.

3. They are accessed by using object pointers.

4. A virtual function can be friend of another class.

5. A virtual function in a class must be defined even though it must not be used.

6. We can not have virtual constructor but we can have virtual destructors.

7. The keyword virtual should not be repeated in the definition if the definition occurs outside the class declaration.

8. A base class pointer can hold the address of the objects of derived classes but pointer to derived class can not hold the address of the object of base class.

#include<iostream.h>
#include<conio.h>
class base
{
public:
void display( )
{
cout<<"\nDisplay Base";
}
virtual void show( )
{
cout<<"\nShow base";
}
};
class Derived:public Base
{
public:
void display( )
{
cout<<"\nDisplay Derived";
}
void show( )
{
cout<<"\n Show derived";
}
};
void main( )
{
Base b;
Derived d;
Base *ptr;
ptr=&b;
ptr->display( );
ptr->show( );
ptr=&d;
ptr->display( );
ptr->show( );
getch( );
}

Output:-
Display Base
  Show base
Display Base
  Show derived

Polymorphism

Polymorphism is one of the crucial features of oop. It is simply means 'one name , multiple forms'. Any operation may exhibit different behaviours in different instances.

Definition:

The ability to take more than one form is known as polymorphism. An operation may exhibit different behaviors in different instances.

Ex: Consider the operation of addition , for two numbers , the operation will generate a sum. If two operands are strings then the operation would produce a third string by concatenation.

Types of polymorphism:
 1. Compiler time polymorphism or Early Binding or Static BInding
 2. Run time polymorphism or Late Binding or dynamic Binding

Compile Time Polymorphism
 1. Selecting a function in normal way during compilation time is known as compile time polymorphism or static binding.

 2. During compilation time , the cpp compiler determines which function is used , based on parameters passed to the function.

 3. Static binding is achieved by using function overloading and operator overloading.

 4. Overloading refers to the same thing for different purpose , in function overloading. We can use the same function name to perform variety of task.

#include<iostream.h>
#include<conio.h>
double volume (double l, double b, double h )
{
return(l*b*h);
}
double volume(double r, double h)
{
return(3.14*r*r*h);
}
void main( )
{
cout<<"\n\tVolume of the box is→"<<volume(1.2,2.2,3.4);
cout<<"\n\tVolume of the cylinder is→"<<volume(1.2,4.2);
getch( );
}

Output:-
Volume of the box is→8.976
Volume of the cylinder is→18.99072

Run Time Polymorphism

 . In some situation, where the function name and the prototype is the same in both the base and derived classes, since the prototype of function is the same in both the places, the function is not overloaded and therefore static binding does not apply.

2. In such cases , the appropriate member function is selected while program is running and this is known as run time polymorphism.

3. Virtual function is used to achieve run time polymorphism.

4. At run time , when it known what class objects are  under consideration the appropriate function is done dynamically at run time.

5. Dynamic binding requires the use of pointers to objects.

Destructor execution in Inheritance

Destructor in Inheritance

As we know destructor are used to free memory which is occupied by the object. A we have seen , the constructor are executed from base class to the derived class. In contrast the destructor are executed in the reverse order that is from most derived class to the base class.

#include<iostream.h>  
#include<conio.h>
 class A
 {
 public:
 ~A( )
 {
 cout<<"\n\n\t Hi I am from---> A";
}
  };
  class B:public A
  {
public:
  ~B( )
  {
  cout<<"\n\n\t Hi I am from---> B";
}
  };
  class C:public B
  {
  public:
  ~C( )
  {
  cout<<"\n\n\t Hi I am from---> C";
  }
  };
  void main( )
  {
  //clrscr( );
  C c1;  
getch( );
  }
 

Output:-
 Hi I am from---> A
 
Hi I am from---> B
 
Hi I am from---> C

Constructor execution in Inheritance

We know that constructor are used to initialize object. In the inheritance without initializing the base class members we can not derive the members of the base class to its derive class.

Therefore base class constructor are executed and then derive class constructor is executed. We must note here that if the base class constructor does not take any parameters then it is not necessary for the derived class to have a constructor.

In such situation , the base class constructor is called first and then the derived class constructor is executed.

#include<iostream.h>
#include<conio.h>
class A
{
public:
A( )
{
cout<<"\n\n\t Hi I am from A";
}
};
class B:public A
{
public:
B( )
{
cout<<"\n\n\t Hi I am from B";
}
};
class C:public B
{
public:
C( )
{
cout<<"\n\n\t Hi I am from C";
}
};
void main( )
{
clrscr( );
C c1;
getch( );
}

Output:-
Hi I am from A

Hi I am from B

Hi I am from C

Use for Heading and Paragraph

This is used for give a heading and paragraph of Website design.

Syntax:-
<h> This is Heading 1  </h>    This is a heading tag.
<p> This is a Paragraph1 </p>  This is a Paragraph tag.

Example:-
<html>

<head>

<title>Irawen </title>

</head>

<body>

<h1>Heading1</h1>

<h2>Heading2</h2>

<h3>Heading3</h3>

<h4>Heading4</h4>

<p>This is a first Paragraph</p>

<p>This is a second Paragraph</p>

<p>This is a third Paragraph</p>

<p>This is a fourth Paragraph</p>

</body>

</html>

Output:-
 

Introduction of HTML

What is HTML? 
    HTML stands for Hypertext  Markup Language , Which is used to design HTML Web pages or Templates.

Why it is called Markup Language? 
    Because it is set of markup  tags .These tags are surrounded by angle brackets.

Requirements to design HTML page?
   Browser + Text editor

How to use a HTML?

  • HTML program is always save as Filename.html
  • It is written as a notepad and notepad++.
Example:-
  <html>      //This is open tag.
  <head>
  <tittle> Welcome </tittle>     //This is write a Tittle of Website.
  </head>
  </html>  This is a closed tag.

—>This is a simple code of html.

Virtual Base Class

Consider a situation where multilevel multiple and hierarchical all the three kinds are inherited.



In the above figure the base class is inherited by both Derived1 and Derived2. Derived 3 directly inherits both Derived and Derived2.

All the public and protected member of base are inherited into Derived3 twice through both Derived 1 and Derived2.

Therefore Derived3 would have duplicate sets of members inherited. This causes ambiguity when member of base is used by Derived.

To Resolve this ambiguity, cpp includes a mechanism by which only one copy of base will be included in derived3. And this feature is called as virtual base class.

When a class is made virtual, cpp takes necessary care to inherit only only one copy of the class.

The keyword virtual precedes the base class access specifiers when it is inherited by derived class.

Ex:
Class Base
{

};
Class Derived1 : virtual public Base
{

};
Class Derived2 : virtual  public Base
{

};
Class Derived3 : public Derived1, public Derived2
{

};

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getrollno(int n)
{
rollno=n;
}
void display1( )
{
cout<<"\n\n\tRoll no is--->"<<rollno;
}
};
class test:virtual public student
{
protected:
int cpp;
int rdbms;
public:
void getmarks(int c, int r)
{
cpp=c;
rdbms=r;
}
void display2( )
{
cout<<"\n\n\tMarks of cpp is--->"<<cpp;
cout<<"\n\n\tMarks of rdbms is--->"<<rdbms;
}
};
class spots:virtual public student
{
protected:
int sportwt;
public:
void getsportmark(int s)
{
sportwt=s;
}
void display3( )
{
cout<<"\n\n\tSport Marks is--->"<<sportwt;
}
};
class rest:public test,public sport
{
protected:
int total;
public:
void calresult( )
{
total=cpp+rdbms+sportwt;
}
void display( )
{
display1( );
display2( );
display3( );
cout<<"\n\n\tTotal Result is--->"<<total;
}
};
void main( )
{
result R;
clrscr( );
R.getrollno(101);
R.getmarks(78,87)
R.getsportmark(10);
R.calresult( );
R.display( );
getch( );
}

Output:-
Roll no is--->101
Marks of cpp is--->78
Marks of rdbms is--->87
Sport Marks is--->10
Total Result is--->175

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (115) C (77) C# (12) C++ (82) Course (62) Coursera (178) coursewra (1) Cybersecurity (22) data management (11) Data Science (91) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (747) Python Coding Challenge (202) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses