You are on page 1of 26

Labe activities

Subject:

OBJECT ORIENTED PROGRAMING IN C++ (OOP)

Submitted by:

Name: Nawsher Ali

Roll No: 1864114

Semester: 2nd

Submitted to:

Mr. Abdullah khan

Date: May, 6 2019

Govt AKL PG College Matta Swat

Department of Computer Science


Code activity No 1.

//Unary operator overlaoding

#include <iostream.h>

#include <conio.h>

class unary //class name unary

private:

int count; //Declaration of veriable count

public:

unary () : count (0) {} //Constructor initialize count

void operator ++ () //Function unary (++) operator

cout << " ++ count " << count++; //increment and show count

} //End of function

}; //End of class

void main() //Main function

unary obj ; //unary class object

clrscr () ;

++ obj ; //call to function (++)

++ obj ;

getch () ;

} //End of programe
Out put

Code activity No 2.

//Programe for Nameless object

#include <iostream.h>

#include <conio.h>

class counter //class counter

private:

int count; //Declaration veriable count

public:

counter () : count (0) {} //costructor that initialize count

counter (int c) : count (c) {} //consructor to that pass veriable....(A)

int showcount () //function that return count

{ return (count); } //End of function

counter operator ++() //Function that increment count and return object
{

++count;

return counter(count); //there become Nameless object that passing to (A) consructor

} //End of function

}; //End of class

void main() //Main Function

clrscr();

counter c1 , c2 ; //Two objects of class counter

cout << " \n C1 \t = \t " << c1.showcount () ; //call to show functions

cout << " \n C2 \t = \t " << c2.showcount () ;

++c1; //call to function that increment count

c2=++c1; //initialize function to C2 object

cout << " \n C1 \t =\t " << c1.showcount () ; //again call to show function

cout << " \n C2 \t = \t " << c2.showcount () ;

getch () ;

} //End of programe

Out put
Code activity no 3

// Addition of two objects by binary operator (+)

#include <iostream.h>

#include <conio.h>

class add //class for addition two objects

private : //Veriables Declared

int x , y , z ;

public:

add () : x (10) , y (20) , z (30) {} //constructor initialize veriables

add ( int a, int b , int c ) : x (a) , y (b) , z (c) {} //Perameters passing to consructor

void operator + ( add ob ) //Object pass to Function

cout << " sum of x = \t " << this - > x + ob . x ; // Add veriable current object

cout << " \n sum of y = \t " << this - > y + ob . y ; //and passed object

cout << " \n sum of z = \t " << this - > z + ob . z ;

}
};

void main() // main function

clrscr () ; //objects of add class

add obj1 ;

add obj2 ( 50 , 60 , 70 ) ;

obj1 + obj2 ; //add object by (+)

getch () ;

} //End of programe

OUT PUT

Code activity no 4

//Programe for addition of three objects

#include <iostream.h>

#include <conio.h>

class add //class add

private:

int x , y ; //Declaration of variables

public:
add () : x (0) , y (0) {} //Constructor with no perameters

add ( int a , int b ) : x (a) , y (b) {} //Perameterized constructor

void getdata () //Function for get data

cout << " Enter x \t " ; cin >> x ;

cout << " \n Enter y \t"; cin >> y;

} //End of function

void showdata () const //constant Function that show data

cout << " \n x = \t " << x ;

cout << " \ ny = \t " << y ;

} //End of function

add operator + ( add ob ) //Function overloading operator (+) And pass object

int x1= this-> x + ob . x ;

int y1 = this-> y + ob.y ; //Add veriable of current object and passing

return add ( x1 , y1 ) ;

} //End of Function

}; //End of class

void main () //Main Function

clrscr () ;

add obj1 ; //Become objects

add obj3 ;
add obj4 ;

obj1 . getdata () ; //call to getdata function

add obj2 ( 50 , 60 ) ; //passing perameter to constructor

obj3 = obj1 + obj2 ; //Add two objects

obj4 = obj1 + obj2 + obj3 ; //Add three objects

cout << " \n \n obj1 = \t " ;

obj1 . showdata () ; cout << endl ; //call to showdata function one by one

cout << " \n obj2 = \t " ;

obj2 . showdata () ; cout << endl ;

cout << " \n obj3 = \t " ;

obj3 . showdata () ; cout << endl ;

cout << " \n obj4 = \t " ;

obj4 . showdata () ; cout << endl ;

getch () ;

} //End of Programe

Out put

Code activity no 5
//mathod overridindg

#include <iostream.h>

#include <conio.h>

class first //declaration parent base

public:

void showF ( ) //write function show First

cout << " Show Function of First " << endl ;

} //End of Function

}; //End of class

class second : public first //Child inherit parent class publicaly

public:

void showF () //Here we override the Function First

cout << " Orriden Functoin of base class (parent) " << endl ;

first :: showF () ; //Acces show function by class name scope & (::)

} //End of Function

}; //End of class

void main () //Main Function

clrscr () ;

first obj1 ; //creat object of parent class


second obj2; //creat object of child class

obj1.showF () ; //call to show function of parent class

obj2.showF () ; //call to show function of child class

getch () ;

} //End of programe

Out put

Code activity no 6

//Acces combination

#include <iostream.h>

#include <conio.h>

class A //class parent

private: int privA ; //declaration of veriables

protected: int protA ;

public: int pubA ;

// privA = 10 ;

A () : privA (10) , protA (20) , pubA (30) {} //initialize veriable by costructor


}; //End of parent class

class B : public A //child class publicaly inherit parent

public :

void showData () //Function in child that show data of parent class

// cout << " Private Data of class A (parent) " << privA << endl ;

//The private data of parent class not accessible in child therefor commented

cout << " Protected Data of class A (parent) \t " << protA << endl ;

cout << " Public Data of class A (parent) \t \t " << pubA << endl ;

} //End of Function

}; //End of programe

void main() //Main Function

clrscr () ;

B obj ; //Object of Child class

obj . showData () ; //call to Show Data Function

getch () ;

} //End of programe

Out put
Code activity no 7

//virtual Function

#include <iostream.h>

#include <conio.h>

class base //class Parent

public :

virtual void show () //virtual Show Function of base

cout << " Base class show Function " << endl ;

} //End of Function

}; //End of class

class derived1 : public base //class child inherit publicaly parent

public :

void show () //show function of child class


{

cout << " Derived1 class show Function " << endl ;

} //End of Function

}; //End of class

class derived2 : public base //child clas inherit parent class

void show () //Show Function of child class

cout << " Derived2 class show Function " << endl ;

} //End of Function

}; //End of class

void main () //Main Function

clrscr () ;

base * ptr [2] ; //pointer to object array of parent class

derived1 ob1 ; //object of child 1 class

derived2 ob2 ; //object of child 2 class

ptr [0] = & ob1 ; //pointer ptr store adress of child 1 class

ptr [1] = &ob 2 ; //pointer ptr store adress of child 2 class

ptr [0] -> show() ; //call to show function child 1 by adress

ptr [1] -> show () ; //pointer ptr store adress of child 2 class

// ptr-> work like keyword [ this -> ]

getch () ;

} //End of programe
Out put

Code activity no 8

//virtual destrutor

#include <iostream.h>

#include <conio.h>

class base //parent class

public:

virtual ~ base () //virtual destructor

cout << " Base class Destroyed " << endl ;

} //End of destructor

}; //End of parent class

class derived : public base //class child publicaly inherit parent class

public:
~ derived () //class child virtual destructor

cout << " Derived class Destroyed " << endl ;

} //End of destructor

}; //End of child class

void main () //main function

clrscr () ;

derived obj ; //object child class

//at the time become object the destructors automatic called

getch () ;

} //End of programe

Out put

Code activity no 9

//solve ambiguity
#include <iostream.h>

#include <conio.h>

class Parent //class parent

public :

void show () //show function of parent class

cout << " Show Function of Parent class " << endl ;

} //End of Function

};

//Remark with out keyword [virtual] there will ambiguos error in Grand child

class Child1 : virtual public Parent //child 1 publicaly inherit virtualy


parent class

{}; //Body of child 1

class Child2 : virtual public Parent //child 2 publicaly inherit virtualy parent class

{};

class G_child : public Child1 , public Child2

//Grand child publicaly inherit class child 1 & 2

{}; //Body of Grand child

void main () //Main Function

clrscr () ;

G_child Object ; //object of grand child class

Object . show () ; //call to show function

getch () ;
} //End of Programe

Out put

Code activity no 10

//Friend function

#include <iostream.h>

#include <conio.h>

class B ; //Declaration of class B

class A //class A

private :

int data ; //Declaration private veriable of class A

public :

A() : data (10) { } //consructor assign value to veriable

friend int F_function ( A , B ) ;

//Declar friend function to which we pass two object of both class

};
class B //class B

private : //Declaration of private veriable of B


class

int data ;

public :

B() : data (20) { } //constructor assign value to veriable

friend int F_function (A ,B) ;

//Declar friend function to which we pass two object of both class

}; //End of class B

int F_function ( A a , B b ) //Definition of Friend function

cout << " Sum of Private data of class A & B = " ;

return ( a . data + b . data) ; //Add private data of both class then return

}; //End of Friend function

void main() //Main Function

clrscr () ;

A ob1 ; //Object of class A

B ob2 ; //Object of class B

cout << F_function ( ob1 , ob2 ) ; //call to freind function

getch () ;

} //End of Programe

Out put
Code activity no 11

//Friend classes

#include <iostream.h>

#include <conio.h>

class beta ; //Declaration of class Beta

class alpha //class alpha

private :

int data1 , data2 ; //declaration of private veriable class alpha

public :

alpha ( ) : data1 (100) , data2 (200) { } //constructor assign value to data veriable

friend class beta ; //Decalration of friend class beta

}; //End of class

class beta //class beta

public :
void showdata1 ( alpha a ) //Function 1 of beta class to which pass object of alpha

cout << " Private data 1 of alpha = " << a .data1<< endl ;//acces private data of alpha class

} //End of 1st Function

void showdata2 ( alpha b ) //Function 2 of beta class to which pass


object of alpha

cout<<"Private data 2 of alpha = "<<b.data2<<endl;//acces the private data of alpha class

} //End of 2nd Function

}; //End of beta class

void main () //Main function

clrscr () ;

alpha obj1 ; //object 1 of alpha class

alpha obj2 ; //object 2 of alpha class

beta ob ; //object of class beta

ob.showdata1 (obj1) ; //call to show function and pass object 1

ob.showdata2 (obj2) ; //call to show function and pass object 2

getch() ;

} //End of Programe

Out put
Code activity no 12

//static veriable and static function

#include <iostream.h>

#include <conio.h>

class info //class name info(information)

private :

static int total ; //static veriable total

int id ; //non_static veriable ID

public :

info () //constructor

total ++ ; //increament total

id = total ; //assign total to ID

} //End Constructor

~ info () //destructor

{
total -- ; //decreament total

cout << "Destroying ID number " << id << endl ;

} //End Destructor

static void showtotal () //static function

cout << "Total is " << total << endl;

} //End function

void showd ( ) //non-static function

cout << " ID number is " << id << endl ;

} //End function

}; //End of class

int info :: total = 0 ; //declaration of static total

void main () //Main function

clrscr () ;

info g1 ; //Became objects of class info

info :: showtotal ( ) ; //call to static function via scope resolution operator

info g2 ;

info :: showtotal () ;

info g3 ;

info :: showtotal () ;

g1 . showid () ; //call to non-static function through objects

g2 . showd () ;
g3 . showid () ;

cout << "----------end of program----------\n";

getch () ;

Out put

Code

//Multiple student record in file handling

#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <stdio.h>

class student //Declaration of class

private:

char Name[15]; //Declaration of veriables


long int R_No;

int Marks;

public:

void getdata() //Function input student record

cout<<"\nEnter Name\t";gets(Name);

cout<<"\nEnter Roll Number\t";cin>>R_No;

cout<<"\nEnter marks\t";cin>>Marks;

} //End of Function

void showdata() //Function show student data

cout<<"\n\nName ... "<<Name;

cout<<"\nRoll Number ... "<<R_No;

cout<<"\nMarks ... "<<Marks;

} //End of Function

}; //End of class

void main() //Main Function

student std; //student class object

clrscr();

char ch;

fstream file; //create input/output file

file.open("std.DAT", ios::app | ios::out | //open for append

ios::in | ios::binary );
do //data from user to file

cout << "\nEnter Student data:";

std.getdata(); //get oneby one student data write to file

file.write((char*)(&std), sizeof(std) );

cout << "Enter another (y/n)? ";

cin >> ch;

while(ch=='y'); //quit on 'n'

file.seekg(0); //reset to start of file

int x=1;

file.read((char*)(&std), sizeof(std) ); //read first student

while(!file.eof())

cout << "\nStudent:"; //display student

std.showdata(); //read another student

file.read((char*)(&std), sizeof(std) );

x++;

getch();

Input data :
out put data:

You might also like