NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

LAB 1

OBJECTIVE: To illustrate the concept of Classes and Objects

THEORY:

Objects and classes are fundamental concepts in object-oriented programming (OOP), a popular paradigm in software development. They help organize and structure code by modeling real-world entities, making it easier to create and manage complex systems.

1. Class:
Definition: A class is a blueprint or template for creating objects. It defines the attributes (data members) and methods (functions) that the objects of the class will have.
Example: Consider a "Car" class. It could have attributes like "color," "make," "model," and methods like "start_engine" and "stop_engine."

2. Object:
Definition: An object is an instance of a class. It is a concrete realization of the class's blueprint, with its own unique data and behavior. Objects can be thought of as individual entities based on the class's definition.
Example: If we create two car objects from the "Car" class, one might be a red Toyota Camry, and the other could be a blue Honda Accord. Each of these objects would have its own specific color, make, and model while sharing common methods.



SOURCE CODE:
#include<iostream>
using namespace std;
class Person{
private:
int age;
string name;
public:
void setName(int a, string n){
age = a;
name = n;
}
void getName(){
cout<<"my name is "<<name<<" and age is "<<age<<endl;
}
};
int main()
{
Person P1,P2;
P1.setName(23,"Sita");
P1.getName();
P2.setName(28,"Ram");
P2.getName();
}

OUTPUT:


CONCLUSION:

In the above program, Person is the class definition, which serves as a blueprint for creating objects that represent individuals with age and name attributes.

P1 and P2 are objects (instances) of the Person class. They represent specific individuals with their own age and name.

The setName method sets the attributes for each person, and the getName method displays those attributes.











LAB 2

OBJECTIVE: To illustrate the concept of function call by
value and call by reference.

THEORY:

In C++, there are two ways to pass arguments to functions while invoking. They are; call by value and call by reference. The main difference between these two methods lies in how they handle the function and parameters and the impact on the original data passed to the function.
Function call by value: While passing argument to a function, the function receives a copy of the actual parameter (value) passed to it. The function operates on the copy of the data, not the original data itself.
Function call by reference: While passing argument to a function, the function receives the reference/address of the actual parameter (value) passed to it. The function operates on the original data itself, not on the copy.

SOURCE CODE:
Function call by value:
#include<iostream>
using namespace std;
void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a= 5, b=6;
cout<<"Before swap:"<<endl;
cout<<"a= "<<a<<" b= "<<b<<endl;
swap(a,b);
cout<<"After swap:"<<endl;
cout<<"a= "<<a<<" b= "<<b<<endl;
return 0;
}
OUTPUT:

Function call by reference:
#include<iostream>
using namespace std;
void swap(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a= 5, b=6;
cout<<"Before swap:"<<endl;
cout<<"a= "<<a<<" b= "<<b<<endl;
swap(a,b);
cout<<"After swap:"<<endl;
cout<<"a= "<<a<<" b= "<<b<<endl;
return 0;
}

OUTPUT:


CONCLUSION:

In function call by value, When the swap function is called, it swaps the copies of a and b, not the actual variables a and b. Therefore, the values of a and b in the main function remain unchanged. The output will show that a and b are the same before and after the swap.

In function call by reference, Since references are used as parameters, the swap function directly modifies the original variables a and b. As a result, the values of a and b are swapped, and the output will show that a and b have been successfully swapped.




LAB 3

OBJECTIVE: To illustrate the concept of function overloading

THEORY:

Function overloading is a programming feature that allows you to define multiple functions with the same name in the same scope, differentiating them by the number or types of their parameters. It enables you to provide different behaviors for the same function name, making your code more versatile and expressive.

Advantages:
• Improved code readability and understanding.
• Flexibility to handle different data types and parameters
• Better abstraction and hiding of implementation details.
• Enables polymorphism and working with multiple types through a common interface.


SOURCE CODE:
#include<iostream>
using namespace std;
int sum(int a, int b)
{
return a+b;
}
int sum(int a, int b, int c)
{
return a+b+c;
}
int sum(double a, double b)
{
return a+b;
}
int main() {
int result1, result2;
double result3;
result1 = sum(5,7);
result2 = sum(1,2,3);
result3 = sum(1.5,5.5);
cout<<"Result 1: "<<result1<<endl;
cout<<"Result 2: "<<result2<<endl;
cout<<"Result 3: "<<result3<<endl;
}
OUTPUT:




CONCLUSION:

In the provided code, function overloading is demonstrated through the sum function, where multiple functions with the same name but different parameter lists exist in the same scope. This enables the program to perform addition operations with flexibility: int sum(int a, int b) calculates the sum of two integers, int sum(int a, int b, int c) computes the sum of three integers, and int sum(double a, double b) finds the sum of two doubles. By calling these overloaded functions with different argument combinations in the main function, the program showcases how function overloading allows you to create versatile functions that adapt to varying data types and argument counts while maintaining a consistent function name, enhancing code readability and adaptability.




















LAB 4

OBJECTIVE: To illustrate the concept of Constructor

THEORY:

In object-oriented programming, a constructor is a special type of method within a class that is automatically called when an object of that class is created. Constructors are used to initialize the object's attributes and set up the object's initial state. Constructors do not have a return type, not even void, and their name is typically the same as the class name.

There are three main types of constructors:

Default Constructor: This constructor is called automatically when an object is created with no arguments. It initializes the object with default values or performs any necessary setup. It does not take any parameters.

Parameterized Constructor: This constructor takes one or more parameters and is used to initialize the object with specific values when it is created. It allows you to customize the initialization of the object based on the provided arguments.

Copy Constructor: The copy constructor is used to create a new object as a copy of an existing object of the same class. Its primary purpose is to initialize a new object with the same values as an existing object. The copy constructor typically takes a single argument, which is a reference to an object of the same class.

SOURCE CODE:
#include<iostream>
using namespace std;
class Test{
private:
int length, breadth;
public:
//Default constructor
Test()
{
cout<<"default constructor is called"<<endl;
}
//Parametarized constructor
Test(int l, int b)
{
cout<<"This is parametarized constructor"<<endl;
length = l;
breadth = b;
}
//Copy constructor
Test(Test &obj)
{
length = obj.length;
breadth = obj.breadth;
cout<<"this is copy constructor"<<endl;
}
void display()
{
cout<<"Length and breadth are "<<length<<" and "<<breadth<<endl;
}
};
int main()
{
Test T1;
Test T2(7,8);
T2.display();
Test T3(T2);
T3.display();
}

OUTPUT:


CONCLUSION:

In this C++ code, there's a Test class with three types of constructors: default, parameterized, and copy constructors. The default constructor initializes the length and breadth attributes when an object is created without arguments. The parameterized constructor allows objects to be created with specific length and breadth values. The copy constructor is used to duplicate the state of an existing object into a new one. In the main function, objects T1, T2, and T3 are created, demonstrating the use of these constructors. T1 is created with the default constructor, T2 with the parameterized constructor and values 7 and 8, and T3 as a copy of T2. The code showcases how constructors are used for object initialization and copying within a class.
This code also shows the example of constructor overloading.
LAB 5
OBJECTIVE: To illustrate the concept of Destructor

THEORY:
Destructors in C++ are special member functions that are responsible for cleaning up resources and performing any necessary cleanup operations when an object goes out of scope or is explicitly destroyed. Destructors have the same name as the class, preceded by a tilde (~). Destructor is a member function that is invoked automatically whenever an object is going to be destroyed.

SOURCE CODE:
#include<iostream>
using namespace std;
class Test{
public:
//userdefined constructor
Test()
{
cout<<"Constructor is executed"<<endl;
}
//userdefined destructor
~Test()
{
cout<<"Destructor is executed"<<endl;
}
};
int main()
{
Test T;
return 0;
}
OUTPUT:


CONCLUSION:
The destructor for the Test class is executed and prints "Destructor is executed" when the Test object T goes out of scope in the main function, indicating the end of its lifetime and allowing for any necessary cleanup.
LAB 6

OBJECTIVE: To illustrate the concept of passing object
as argument and returning object from a function

THEORY:

Object as function argument:
In C++, you can pass objects as function arguments just like any other data type. When you pass an object as an argument to a function, you can access and manipulate the object's data members and call its member functions within the function. This is a fundamental concept in object-oriented programming and allows you to work with objects in a modular and reusable way.

Returning object from a function:
In C++, you can return objects from functions by value, reference, or pointer. Returning an object by value involves creating a copy of the object inside the function and returning that copy. This method is suitable for smaller objects and provides a new, independent instance of the object. However, it can be less efficient for larger or resource-intensive objects due to the overhead of copying. To avoid copying, you can return objects by reference or by pointer, allowing the caller to work with the original object directly. Returning objects by reference or pointer is more efficient but requires careful memory management to prevent issues like dangling pointers. The choice between these methods depends on the specific use case and performance considerations.

SOURCE CODE:
Passing object as a function argument:
#include<iostream>
using namespace std;
class Distance{
private:
int kilometre;
int total;
public:
void setData(int km)
{
kilometre = km;
}
void sum(Distance D1, Distance D2)
{
total = D1.kilometre+D2.kilometre;
}
void display()
{
cout<<"The total distance is "<<total<<endl;
}
};
int main()
{
Distance D1,D2,T1;
D1.setData(78);
D2.setData(80);
T1.sum(D1,D2);
T1.display();
}
OUTPUT:


Returning object from a function:
#include<iostream>
using namespace std;
class Distance{
private:
int kilometre;
int total;
public:
void setData(int km)
{
kilometre = km;
}
Distance sum(Distance D1)
{
Distance T1;
T1.total = kilometre+ D1.kilometre;
return T1;
}
void display()
{
cout<<"The total distance is "<<total<<endl;
}
};
int main()
{
Distance D1,D2,D3;
D1.setData(78);
D2.setData(80);
D3= D1.sum(D2);
D3.display();
}

OUTPUT:


CONCLUSION:

Passing object as a function argument:
In this code example, we have a Distance class with methods for setting data (setData), summing distances (sum), and displaying the total distance (display). The sum function takes two Distance objects (D1 and D2) as arguments and calculates their total distance, which is stored in the total member variable of another Distance object (T1). The display function then displays the total distance.

Returning object from function:
In this code , the Distance class also has methods for setting data (setData), displaying the total distance (display), and a modified sum function. The sum function now takes one Distance object (D1) as a parameter and returns another Distance object (T1) that represents the sum of the distances. The display method is used to display the total distance of the returned object.



















LAB 7

OBJECTIVE: To illustrate the concept of unary and binary operator overloading

THEORY:

In C++, operator overloading allows you to define custom behaviors for operators when applied to user-defined data types, including classes. Unary and binary operator overloading are two aspects of this feature, and they enable you to customize how operators behave when used with your own classes.

Unary Operator Overloading:

Unary operators operate on a single operand. Some common unary operators include ‘+’, ‘-‘, ‘++’ , ‘- - ‘ , ‘ !’ and ~. To overload a unary operator for a class, you define a member function that takes no arguments (besides the implicit this pointer) and has the operator symbol as its name.

Binary Operator Overloading:

Binary operators operate on two operands. Examples of binary operators include ‘+’, ‘-‘, ‘*’, ‘/’, ‘= =’, ‘!=’, ‘<’, ‘>’, etc. To overload a binary operator for a class, you define a member function that takes one argument (besides the implicit this pointer) and has the operator symbol as its name.

SOURCE CODE:
Urany operator overloading:
#include<iostream>
using namespace std;
class sample
{
private:
int x;
int y;
public:
void setData(int a , int b)
{
x=a;
y=b;
}
void display()
{
cout<<"values are "<<x<<" and "<<y<<endl;
}
void operator ++()
{
x=++x;
y=++y;
}
};
int main()
{
sample s;
s.setData(8,9);
cout<<"before function call"<<endl;
s.display();
cout<<"after function call"<<endl;
++s;
s.display();
}



Binary operator overloading:
#include<iostream>
using namespace std;
class Complex{
private:
int r,i;
public:
void setData()
{
cout<<"enter real and imaginary numbers "<<endl;
cin>>r;
cin>>i;
}
void display()
{
cout<<r<<" + "<<i<<"i";
}
Complex operator +(Complex b)
{
Complex c;
c.r = r + b.r;
c.i = i+b.i;
return c;
}
};
int main()
{
Complex C1,C2;
C1.setData();
C2.setData();
Complex C3 = C1 + C2;
cout<<"a= ";
C1.display();
cout<<endl;
cout<<"b= ";
C2.display();
cout<<endl;
cout<<"sum of a and b = "<<endl;
C3.display();
}

OUTPUT:


CONCLUSION:

The provided C++ code showcases operator overloading in two different classes. In the sample class, a unary operator, pre-increment (++), is overloaded to increment both x and y members by 1 when applied to an object of the class. The Complex class demonstrates binary operator overloading for addition (+), where two complex numbers are added together by summing their respective real and imaginary parts, returning a new Complex object. In the main function, instances of both classes are created and manipulated using these custom operator overloads, highlighting how C++ allows you to define specialized behaviors for operators, enhancing the flexibility and expressiveness of user-defined data types.

LAB 8

OBJECTIVE: To illustrate the concept of inheritance and its types

THEORY:

Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and behaviors from another class. Inheritance promotes code reusability and allows you to create new classes based on existing ones. There are several types of inheritance:

Single Inheritance: In single inheritance, a class can inherit properties and behaviors from only one parent class. It creates a simple hierarchy, and the derived class inherits the characteristics of a single base class.

Multiple Inheritance: Multiple inheritance allows a class to inherit from more than one base class. While powerful, it can lead to ambiguities and complexities in code when multiple base classes have conflicting member names or behaviors.

Multilevel Inheritance: In multilevel inheritance, a class is derived from a base class, and then another class is derived from the derived class, creating a chain or hierarchy of inheritance. It helps in organizing and structuring code.

Hierarchical Inheritance: In hierarchical inheritance, multiple derived classes inherit from a single base class. This creates a tree-like structure where multiple classes share common characteristics from the same parent class.

Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance, such as single, multiple, or multilevel inheritance. It provides flexibility but can be complex to manage.

SOURCE CODE:
Single inheritance:
#include<iostream>
using namespace std;
class Animal{
public:
void eat()
{
cout<<"dog eats"<<endl;
}
};
class Dog : public Animal{
public:
void bark()
{
cout<<"dog barks "<<endl;
}
};
int main()
{
Dog D1;
D1.eat();
D1.bark();
}
Output:


Multiple Inheritance:
#include <iostream>
using namespace std;
class Shape {
public:
void display() {
cout << "This is a shape." << endl;
}
};
class Color {
public:
void displayColor() {
cout << "This object has a color." << endl;
}
};
class Table : public Shape, public Color {
public:
void show() {
cout << "This is a colored shape." << endl;
}
};
int main() {
Table T;
T.display();
T.displayColor();
T.show();
return 0;
}
Output:


Multilevel Inheritance:
#include <iostream>
using namespace std;
class Vehicle {
public:
void start() {
cout << "Vehicle started." << endl;
}
};
class Car : public Vehicle {
public:
void drive() {
cout << "Car is being driven." << endl;
}
};

class SportsCar : public Car {
public:
void race() {
cout << "Sports car is racing!" << endl;
}
};

int main() {
SportsCar S;
S.start();
S.drive();
S.race();
return 0;
}
Output:


Hierarchical Inheritance:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "The animal eats food." << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "The cat meows." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};

int main() {
Cat myCat;
Dog myDog;
myCat.eat();
myCat.meow();
myDog.eat();
myDog.bark();
return 0;
}

OUTPUT:


CONCLUSION:

Hence, these types of inheritance allow us to model relationships between classes in different ways, depending on your software design needs, and enable code reuse and organization in object-oriented programming.
LAB 9

OBJECTIVE: To illustrate the concept of private, protected and public keyword in inheritance

THEORY:

Public Inheritance: When a derived class is publicly inherited, the access specifiers of the parent class members are preserved in the child class as well. That means, public and protected members of parent class remain public and protected members in child class as well.

Protected Inheritance: When a child class is derived using protected keyword, ‘protected’
becomes the highest possible access modifier in the child member.

Private Inheritance: When a child class is derived using private keyword, all the public and
protected members of parent class are downgraded to private members in the child class.

SOURCE CODE:
#include <iostream>
using namespace std;
class Animal {
private:
int privateValue = 42;

protected:
int protectedValue = 10;

public:
int publicValue = 5;

void DisplayValues() {
cout << "Private Value: " << privateValue << endl;
cout << "Protected Value: " << protectedValue << endl;
cout << "Public Value: " << publicValue << endl;
}
};

class Dog : public Animal {
public:
void show() {
cout << "Accessing Base Class Values from Dog Class:" << endl;
//cout << "Private Value: " << privateValue << endl; // Error, private member is not accessible
cout << "Protected Value: " << protectedValue << endl;
cout << "Public Value: " << publicValue << endl;
}
};

int main() {
Dog myDog;
myDog.show();
// Accessing base class members directly from main (outside the class hierarchy)
cout << "Accessing Base Class Values from Main Function:" << endl;
// cout << "Private Value: " << myDog.show << endl; // Error, private member is not accessible
// cout << "Protected Value: " << myDog.show << endl; // Error, protected member is not accessible
cout << "Public Value: " << myDog.publicValue << endl;
return 0;
}

Output:




CONCLUSION:

In this example, we have a base class Animal with private, protected, and public members. We then derive a class Dog from Animal to illustrate the access specifiers:

Private members (privateValue) are not accessible outside the class itself.
Protected members (protectedValue) are accessible within the class and by derived classes.
Public members (publicValue) are accessible everywhere, including outside the class hierarchy.








LAB 10

OBJECTIVE: To illustrate the concept of function overriding

THEORY:

As we know, inheritance is a feature of OOP that allows us to create derived classes from a base class. The derived classes inherit features of the base class.

Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed.

This is known as function overriding in C++. The function in derived class overrides the function in base class.

SOURCE CODE:

#include<iostream>
using namespace std;
class Base{
public:
void display()
{
cout<<"this is a base class"<<endl;
}
};
class Derived : public Base{
public:
void display() //function overrding
{
cout<<"this is a derived class"<<endl;
}
};
int main()
{
Derived D;
D.display();
D.Base :: display();
return 0;
}



OUTPUT:



CONCLUSION:

The code defines two classes: Base and Derived. The Derived class inherits from the Base class and overrides the display() function. The main() function creates an object of the Derived class and calls both the Derived and Base class display() functions.
































LAB 11

OBJECTIVE: To illustrate the concept of pure virtual function and abstract class

THEORY:

A pure virtual function is a function in a base class that has no implementation and is meant to be overridden by derived classes. An abstract class is a class that contains at least one pure virtual function and cannot be instantiated. It serves as a blueprint for derived classes to define their own implementations of the pure virtual function.

SOURCE CODE:

#include<iostream>
using namespace std;
class Shape{
public:
virtual void draw()=0; //pure virtual funtion
};
class Circle : public Shape{
public:
void draw()
{
cout<<"This is a circle"<<endl;
}
};
class Rectangle : public Shape{
public:
void draw()
{
cout<<"This is a rectangle"<<endl;
}
};
int main()
{
Circle c;
Rectangle r;
c.draw();
r.draw();
return 0;
}


OUTPUT:



CONCLUSION:

In this example, the Shape class is an abstract class because it contains a pure virtual function called draw(). This means that any class that inherits from Shape must provide its own implementation of the draw() function. The Circle and Rectangle classes inherit from Shape and override the draw() function with their specific implementations.
























LAB 12

OBJECTIVE: To illustrate the concept of runtime polymorphism

THEORY:

Runtime polymorphism is a concept in object-oriented programming where the actual implementation of a method is determined at runtime, based on the type of the object being referenced. It allows a parent class reference variable to refer to an object of its child class, and the appropriate method of the child class is called based on the object's type. This enables dynamic method binding and allows for flexible and extensible code.

In C++, virtual functions enable runtime polymorphism. When a base class declares a function as virtual, it allows derived classes to override that function with their own implementation. When the function is called through a base class pointer or reference, the actual implementation of the function is determined at runtime based on the type of the object being referred to.

SOURCE CODE:

#include<iostream>
using namespace std;
class Animal{
public:
virtual void Sound()
{
cout<<"Animal makes a sound"<<endl;
}
};
class Dog : public Animal{
public:
void Sound()
{
cout<<"Dog barks"<<endl;
}
};
class Cat : public Animal{
public:
void Sound()
{
cout<<"Cat meows"<<endl;
}
};
int main()
{
Animal *a;
Dog d;
Cat c;
a = &d;
a->Sound();
a = &c;
a->Sound();
return 0;
}

OUTPUT:



CONCLUSION:

The code demonstrates runtime polymorphism using virtual functions in C++. A base class, Animal, has a virtual function, Sound(), which is overridden by derived classes, Dog and Cat. By using a base class pointer, the appropriate implementation of Sound() is determined at runtime based on the actual object type.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.