PCPF - PRACTICAL - Manual - Output 2
PCPF - PRACTICAL - Manual - Output 2
Experiment __
Code:
/*
1)
programm in C++ to demonstrate concepts like - Encapsulation, Inheritance, Initialization and
Finalization, Dynamic Binding.
*/
#include <iostream>
#include <string>
using namespace std;
// Encapsulation: Private data members with public getter and setter methods
class Person {
private:
string name;
int age;
public:
// Constructor: Initialization
Person(string n, int a) : name(n), age(a) {
cout << "Person constructor called for " << name << endl;
}
// Destructor: Finalization
virtual ~Person() {
cout << "Person destructor called for " << name << endl;
}
public:
// Constructor: Initialization
Student(string n, int a, string m) : Person(n, a), major(m) {
cout << "Student constructor called for " << getName() << endl;
}
// Destructor: Finalization
~Student() {
cout << "Student destructor called for " << getName() << endl;
}
int main() {
// Using a base class pointer for dynamic binding
Person* p = new Student("Alice", 20, "Computer Science");
return 0;
}
Output:
/*
5)
A c++ Program to understand Exception handling and Garbage collection
*/
#include <iostream>
#include <memory>
#include <exception>
// Destructor
~Resource() {
std::cout << "Resource released." << std::endl;
}
int main() {
try {
// Normal execution
handleResource(false);
// Exception scenario
handleResource(true);
} catch (const CustomException& e) {
std::cout << "Caught Exception: " << e.what() << std::endl;
} catch (const std::exception& e) {
std::cout << "Caught Standard Exception: " << e.what() << std::endl;
}
return 0;
}
Output:
/*
2)
Program in c++ to demonstrate Thread management and synchronization
if facing issue in debugging then, you can add the -pthread flag to the compilation command. Here's
how to compile the program:
*/
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
int main() {
// Vector to hold thread objects
vector<thread> threads;
// Launch 5 threads
for (int i = 0; i < 5; ++i) {
threads.push_back(thread(incrementCounter, i + 1));
}
// Join all threads to the main thread (wait for all threads to finish)
for (auto& th : threads) {
th.join();
}
return 0;
}
Output:
This code involves thread management, so the output would involve messages from multiple
threads completing tasks concurrently.
Experiment __
Code:
/*
4)
A java Program to understand Exception handling and Garbage collection
*/
class Resource implements AutoCloseable {
// Constructor
public Resource() {
System.out.println("Resource acquired.");
}
Output:
/*
3)
Program in java to demonstrate Thread management and synchronization
*/
class Counter {
private int count = 0;
// The run method contains the code that the thread will execute
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
counter.increment();
}
System.out.println("Thread " + threadId + " finished execution.");
}
}
public class ThreadSynchronizationDemo {
public static void main(String[] args) throws InterruptedException {
// Shared resource (counter object)
Counter counter = new Counter();
Output:
https://www.onlinegdb.com/online_haskell_compiler
main :: IO ()
main = do
putStrLn ("The factorial of " ++ show number ++ " is " ++ show (factorial number))
1. Recursion:
o The factorial function calls itself recursively to calculate the factorial of a given
number n.
o The base case is defined for n = 0, where factorial 0 = 1, and the recursive case
is defined as n * factorial (n - 1).
2. Pattern Matching:
o Haskell uses pattern matching to define different behaviors for different inputs.
Here, factorial 0 is a pattern that matches the base case, and factorial n matches
any other number n.
3. Immutability:
o Haskell variables, like number, are immutable. Once assigned, their values
cannot be changed.
4. Pure Functions:
o The factorial function is a pure function, meaning its output depends only on its
input, and it has no side effects.
5. I/O Operations:
o The main function uses putStrLn to display messages and getLine to read user
input. The input is processed by reading and converting it to an integer, and the
result is displayed using string concatenation.
How to Run the Program:
./factorial
Explanation:
The program prompts the user to input a number.
The factorial function computes the factorial of that number using recursion.
The result is printed to the console.
Conclusion:
This Haskell example introduces important functional programming concepts like recursion,
pattern matching, immutability, and pure functions, while also showing basic I/O operations.
Experiment __
2) Haskell Programming
In Haskell Implement safetail function that behaves in the same way as tail, except that safetail
maps the empty list to the empty list, whereas tail gives an error in this case. Define safetail
using: (a) a conditional expression; (b) guarded equations; (c) pattern matching. Hint: the library
function null :: [a]-> Bool can be used to test if a list is empty.
https://www.onlinegdb.com/online_haskell_compiler
safetail_guarded xs
| null xs = []
| otherwise = tail xs
safetail_pattern [] = []
safetail_pattern (_:xs) = xs
main = do
Key Changes:
1. Type Annotations:
o I added type annotations like 1 :: Integer to specify that the lists are of type
Integer. This allows the compiler to infer the types correctly.
o For the empty list, ([] :: [Integer]) explicitly defines the type of the empty list.
./main
Expected Output:
[2,3]
[]
[2,3]
[]
[2,3]
[]
Experiment __
3) Haskell Programming
Here’s the complete Haskell program that defines both the addition and multiplication functions:
https://www.onlinegdb.com/online_haskell_compiler
add 0 b = b
add a b = 1 + add (a - 1) b
main :: IO ()
main = do
let a = 5
let b = 3
Addition Function:
o add 0 b = b: If the first number is 0, return the second number (identity of
addition).
o add a b = 1 + add (a - 1) b: Otherwise, add 1 to the result of adding a - 1 to b.
Multiplication Function:
o multiply 0 _ = 0: If the first number is 0, return 0 (anything multiplied by 0 is 0).
You can save the code in a file named Main.hs and then compile and run it using GHC:
bash
./main
Expected Output
15
24
This program effectively uses recursion to perform multiplication through repeated addition.
Experiment __
Prolog programming
Prolog is a logic programming language that is particularly well-suited for tasks that involve
reasoning and knowledge representation. Its declarative nature allows programmers to express
the logic of a computation without explicitly describing its control flow. Here are some simple
Prolog programs that illustrate various declarative programming concepts:
This example defines simple facts and rules about family relationships.
% Facts
% Rules
female(mary).
female(susan).
male(john).
male(sam).
Concepts Illustrated:
Facts: These are the basic assertions about the world.
Rules: These define relationships based on existing facts.
Queries: You can ask questions like mother(susan, mary). to check if Susan is Mary’s
mother.
length([], 0).
length([_|Tail], Length) :-
length(Tail, TailLength),
Length is TailLength + 1.
Concepts Illustrated:
Recursion: The function calls itself to process the tail of the list.
Base Case: Important for terminating the recursion.
List Handling: Prolog's native support for lists.
3. Backtracking
This example demonstrates Prolog’s ability to search through possible solutions.
likes(mary, ice_cream).
likes(mary, chocolate).
likes(sam, pizza).
likes(sam, ice_cream).
Concepts Illustrated:
Backtracking: Prolog automatically searches for all possible values of Y that satisfy the
likes predicate for mary.
Logical Variables: Variables like X can take on different values during the search
process.
edge(a, b).
edge(b, c).
edge(a, d).
edge(d, e).
Concepts Illustrated:
5. Knowledge Representation
This example uses Prolog to represent and query knowledge about animals.
animal(dog).
animal(cat).
animal(bird).
mammal(dog).
mammal(cat).
bird(bird).
is_mammal(X) :- mammal(X).
To run these Prolog examples, you can use various environments such as SWI-Prolog, GNU
Prolog, or online Prolog interpreters. Simply enter the facts and rules, then run queries to see
how Prolog responds.
https://www.onlinegdb.com/online_prolog_compiler
Conclusion
These examples demonstrate some core concepts of declarative programming in Prolog, such
as defining facts and rules, recursion, backtracking, and knowledge representation. You can
expand on these ideas to explore more complex logic programming tasks! If you have any
specific concepts you'd like to explore further, feel free to ask!