CPPL PRACTICAL:
1) SINGLE INHERITANCE USING C++
#include<iostream>
using namespace std;
class staff
string name;
int empId;
string pos;
public: void getdata()
cout<<"Enter staff name: "<<endl;
cin>>name;
cout<<"Enter staff id: "<<endl;
cin>>empId;
cout<<"Enter position: "<<endl;
cin>>pos;
void display()
cout<<"Name of staff is: "<<name<<endl;
cout<<"Staff id is: "<<empId<<endl;
cout<<"Position of staff is : "<<pos<<endl;
};
class typist: public staff
{};
int main()
typist t1;
t1.getdata();
t1.display();
return 0;
2) MULTIPLE INHERITANCE USING C++
#include<iostream>
using namespace std;
class Area{
public: int getArea(int l, int b){
return l*b;
};
class Perimeter{
public: int getPerim(int l, int b){
return 2*(l+b);
};
class Rect: public Area,public Perimeter{
int len;
int bth;
public: Rect(){
len=8;
bth=10;
int area(){
return Area:: getArea(len,bth);
int perim(){
return Perimeter::getPerim(len,bth);
};
int main(){
Rect r;
cout<<"The area is: "<<r.area()<<endl;
cout<<"Perimeter is: "<<r.perim()<<endl;
return 0;
3) HEIRARCHICAL INHERITANCE USING JAVA
class Employee {
int salary = 100000;
class Realemp extends Employee {
double hike = 0.35;
class Fakeemp extends Employee {
double hike = 0.20;
}
public class Salary {
public static void main(String[] args) {
System.out.println("Hello");
Realemp t = new Realemp();
Fakeemp f = new Fakeemp();
System.out.println("Salary of Permanent employee: " + (t.salary + t.salary * t.hike));
System.out.println("Salary of Temporary Employee: " + (f.salary + f.salary * f.hike));
4) REVERSE OF STRING USING C++
#include<iostream>
#include<string>
using namespace std;
class Strings{
protected: string input;
public: void getInput(){
cout<<"Enter a string: ";
getline(cin,input);
void reverse(){
int len=input.length();
for(int i=len-1;i>=0;i--){
cout<<input[i];
cout<<endl;
};
class Reverse: public Strings{
public: void displayRev(){
cout<<"Reversed String: ";
reverse();
};
int main(){
Reverse r;
r.getInput();
r.displayRev();
return 0;
}
5) DATA ENCAPSULATION USING JAVA
public class Bold {
String name;
int age;
void setname(String xname) {
name = xname;
String getname() {
return name;
void setage(int xage) {
age = xage;
int getage() {
return age;
public static void main(String[] args) {
System.out.println("Hello World");
Bold b1 = new Bold();
b1.setname("John ");
System.out.println("Name is: " + b1.getname());
b1.setage(19);
System.out.println("Age is: " + b1.getage());
Bold b2 = new Bold();
b2.setname("Lucy");
System.out.println("Name is: " + b2.getname());
b2.setage(34);
System.out.println("Age is: " + b2.getage());
6) DATA ABSTRACTION USING JAVA
abstract class Animal {
abstract void atype();
void action() {
System.out.println("Animal eats meat");
class Dog extends Animal {
void atype() {
System.out.println("This is a dog");
void sound() {
System.out.println("Animal barks");
public class Light {
public static void main(String[] args) {
System.out.println("Hello");
Dog d = new Dog();
d.sound();
d.action();
d.atype();
7) AREA OF RECT, CIRCLE, SQUARE USING JAVA
import java.util.Scanner;
abstract class Shape {
double len;
double bth;
double rad;
double side;
public abstract double RectArea(double len, double bth);
public abstract double CircleArea(double rad);
public abstract double SquareArea(double side);
class Area extends Shape {
public double RectArea(double len, double bth) {
return len * bth;
public double CircleArea(double rad) {
return (Math.PI * rad * rad);
public double SquareArea(double side) {
return side * side;
}
public class Koala {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Area a = new Area();
Area b = new Area();
Area c = new Area();
System.out.println("Enter Length of Rect: ");
a.len = sc.nextDouble();
System.out.println("Enter Breadth of Rect: ");
a.bth = sc.nextDouble();
System.out.println("Area of Rect is: " + a.RectArea(a.len, a.bth));
System.out.println("\n");
System.out.println("Enter radius of circle: ");
b.rad = sc.nextDouble();
System.out.println("Area of Circle is: " + b.CircleArea(b.rad));
System.out.println("\n");
System.out.println("Enter side of square: ");
c.side = sc.nextDouble();
System.out.println("Area of square is : " + c.SquareArea(c.side));
sc.close();
8) DISPLAY “HELLO SFIT” USING HASKELL
main :: IO()
main = putStrLn("Hello SFIT")
9) MATH OPERATION SQRT(36)+SQRT(25) IN HASKELL
main :: IO()
main = do
let num1 = 36
let num2 = 25
let sqrtSum = sqrt num1 + sqrt num2
putStrLn("The square root of "++show num1++" is "++show(sqrt num1))
putStrLn("The square root of "++show num2++" is "++show(sqrt num2))
putStrLn("The sum of square root is:" ++show sqrtSum)
10) ADD, SUBT, MULTIPLY, DIV NUMBERS IN HASKELL
add1::Int -> Int -> Int let a=0
add1 x y=x+y let b=0
subt1:: Int->Int-> Int putStrLn("Enter 1st num: ")
subt1 x y=x-y a1<-getLine
mult1::Int->Int->Int let a=read(a1)
mult1 x y=x*y putStrLn("Enter 2nd num: ")
div1::Double->Double->Double b1<-getLine
div1 x y= x / y let b=read(b1)
exp1::Int->Int->Int print(add1 a b)
exp1 x y=x^y print(subt1 a b)
mod1::Int->Int->Int print(mult1 a b)
mod1 x y =x `mod` y print(exp1 a b)
main::IO() print(div1 a.0 b.0)
main=do print(mod1 a b
11) APPLY TWICE, MULT THREE AND APPLY STRING USING HASKELL
applyTwice::(a->a)->(a->a)->a->a multThree::Int->Int->Int->Int applyString::String->String->String
applyTwice f1 f2 x= f2(f1 x) multThree x y z= x*y*z applyString s1 s2=s1++s2
main=do main=do main=do
print(applyTwice(+5)(*10)6) print(multThree 3 4 5) print(applyString("Hello ")("World"))
12) FACTORIAL AND REVERSE STRING RECURSION USING HASEKLL
facto::Int->Int revString::String->String
facto 0=1 revString [x]=[x]
facto n=n*facto(n-1) revString (x:xs)=revString xs++[x]
main=do main=do
print(facto 5) print(revString"bunny")
13) DIVIDE BY TEN USING HASKELL
divide::Double->Double
divide x=x/10.0
main::IO()
main=do
let num=500
print("dividing "++show num++" by 10 = "++show(divide num))
14) EVALUATE AREA OF CIRCLE AND TRIANGLE USING HASKELL
triangle::Double->Double->Double
triangle b h=0.5*b*h
circle::Double->Double
circle r=pi*r^2
main::IO()
main=do
let b=5.0
let h=4.0
let r=6.0
putStrLn("Area of Triangle "++show(triangle b h))
putStrLn("Area of Circle "++show(circle r))
15) DOUBLE PASS , SPACES,TRIPLES AND FACTOR IN HASKELL
doublePass::[Int]->[Int] spaces::Int->String
doublePass xs=[2*x | x <- xs ,x>0] spaces n=[' ' | x <- [1..n]]
main=do main=do
print(doublePass [2,-3,4,-2,3,-7,6,8,-4,9]) print(spaces 10)
triples::Int->[(Int,Int,Int)]
triples n=[(x,y,z) | x <-[1..n],y <- [1..n], z <- [1..n], x^2 + y^2 == z^2]
main=do
print(triples 10)
facts::Int->[Int]
facts n=[x | x <-[1..n],(n`mod` x==0) &&(x/=1) && (x/=n)]
main = do
print(facts 20)
16) SNIPPET TO PRINT 10 NUM and FIBONACCI IN HASKELL
nums::[Int] fibs::[Integer]
nums=[1..10] fibs=0:1:zipWith(+)fibs(tail fibs)
main::IO() main::IO()
main=do main=do
putStrLn("List of first 10 numbers: ") putStr("Fibonacci Series: ")
print(nums) print $ take 15 fibs
17) KNOWLEDGE BASE AND QUERIES, AND, OR, EXOR, NOT, NAND IN PROLOG
Consult saved file : consult(‘file_name.pl’). check true or false by asking queries
Save knowledge in knowledge base or file and continue with queries on prolog prompt.
and1(true,true):-true.
?- and1(true,true).
true
18) CONCAT 2 LISTS USING PROLOG
list_concat( [] , L ,L).
list_concat([X1 | L1] ,L2, [X1 | L3] ):-list_concat (L1 ,L2 ,L3).
19) DELETE ITEM FROM LIST USING PROLOG
list_delete(X, [X], []).
list_delete(X, [X | L1], L1).
list_delete(X, [ Y | L2], [Y | L1]) :- list_delete (X ,L1 ,L2).
20) INSERT ITEM INTO LIST USING PROLOG
list_delete(X, [X], []).
list_delete(X, [X | L1], L1).
list_delete(X, [Y | L2], [Y | L1] ) :-list_delete(X, L1, L2).
list_insert( X, L, R) :- list_delete( X, R, L).
21) PALINDROME AND SUBSET OF LISTS USING PROLOG
palindrome ( L ):- reverse( L, L).
subset(L, L).
subset(L, R):-subset1(L, R).
subset1([_|Tail], R):-subset(Tail, R).
subset1([Head|Tail],[Head|R]):-subset1(Tail,R).
22) FACTORIAL USING RECURSION IN PROLOG
factorial(1,1).
factorial(N,Res):-M is N-1,
factorial(M,NextRes),Res is NextRes*N.
23) FIBONACCI SERIES IN PROLOG
fibo(0,0).
fibo(1,1).
fibo(N,Res):-N>1, N1 is N-1, N2 is N-2,
fibo(N1,Res1),fibo(N2,Res2),
Res is Res1+Res2.
fibonacci(N):-between(0,N,I),fibo(I,Res),
write(Res), write(' '),fail.
fibonacci(_).
24) SUM OF FIRST N NATURAL NUMS IN PROLOG
sum (0,0).
sum (N, S):-( N > 0 ->( N1 is N-1, sum( N1, S1), S is N+S1);
N<0 -> (N1 is N+1, sum (N1, S1), S is N+S1)).
25) KNOWLEDGE BASE/ DATABASE MANIPULATION
Directly give commands without creating file :assert, asserta, assertz, retract commands
assert(dog(fido)). Assert(dog(juno)). Assert(animal(X):-dog(X)).
?- Animal(X).
fido;
juno.
26) “HELLO WORLD ” AND “HELLO SFIT ” THREADS USING JAVA
class Den1 extends Thread {
public void run() {
System.out.println("Welcome to SFIT");
class Den2 extends Thread {
public void run() {
System.out.println("Welcome to INFT");
public class Denn {
public static void main(String[] args) {
System.out.println("Hello World");
Den1 d1 = new Den1();
Den2 d2 = new Den2();
d1.start();
d2.start();
27) THREAD USING RUNNABLE INTERFACE IN JAVA
class Renn1 implements Runnable {
public void run() {
System.out.println("Its a runnable Thread");
public class Renn {
public static void main(String[] args) {
System.out.println("Hello World");
Renn1 r = new Renn1();
Thread t1 = new Thread(r);
t1.start();
28) DISPLAY THREAD IDS USING JAVA
import java.util.*;
class Iden4 extends Thread {
public void run() {
System.out.println("Id of thread is: " + Thread.currentThread().threadId());
public class Identity {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter thread: ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
Iden4 x = new Iden4();
x.start();
sc.close();
29) FUNCTIONS USING THREAD IN C++
#include<iostream>
#include<thread>
using namespace std;
void fnc1(){
cout<<"Thread 1 is running\n"<<endl;
void fnc2(){
cout<<"Thread 2 is running"<<endl;
int main(){
cout<<"Hello World"<<endl;
thread t1(fnc1);
t1.join();
thread t2(fnc2);
t2.join();
return 0;
30) DISPLAY THREAD IDS IN C++
#include<iostream>
#include<thread>
using namespace std;
void fnc1(){
cout<<"Thread 1 is running"<<endl;
void fnc2(){
cout<<"Thread 2 is running"<<endl;
int main(){
cout<<"Hello World"<<endl;
thread t1(fnc1);
thread t2(fnc2);
thread::id x=t1.get_id();
thread::id y=t2.get_id();
cout<<"Id of 1st thread: "<<x<<endl;
cout<<"Id of 2nd thread: "<<y<<endl;
t1.join();
t2.join();
return 0;
}
31) THREAD SYNCHRONIZATION IN JAVA
public class Sync {
public static void main(String[] args) {
Table a = new Table();
Mythread1 t1 = new Mythread1(a);
Mythread2 t2 = new Mythread2(a);
t1.start();
t2.start();
class Table {
synchronized public void print(int n) {
for (int i = 1; i <= 5; i++) {
System.out.println(n + " * " + i + " = " + (n * i));
try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
class Mythread1 extends Thread {
Table t;
Mythread1(Table T1) {
this.t = T1;
public void run() {
t.print(4);
class Mythread2 extends Thread {
Table t;
Mythread2(Table T2) {
this.t = T2;
public void run() {
t.print(13);
} }
32) EXCEPTION HANDLING USING TRY CATCH IN JAVA
public class Except {
public static void main(String[] args) {
try {
int n = div(10, 0);
System.out.println("Result: " + n);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception: " + e.getMessage());
public static int div(int x, int y) {
try {
return x / y;
} catch (ArithmeticException e) {
throw e;