0% found this document useful (0 votes)
11 views

sodapdf-converted

The document contains multiple C++ code snippets demonstrating various programming concepts such as classes, constructors, friend functions, and different types of function calls (call by value, call by reference, call by address). Each snippet illustrates a specific feature, including the use of static members, inline functions, and dynamic memory management. Overall, the document serves as a practical guide to understanding object-oriented programming in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

sodapdf-converted

The document contains multiple C++ code snippets demonstrating various programming concepts such as classes, constructors, friend functions, and different types of function calls (call by value, call by reference, call by address). Each snippet illustrates a specific feature, including the use of static members, inline functions, and dynamic memory management. Overall, the document serves as a practical guide to understanding object-oriented programming in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

/*#include <iostream>

int main()
{
std::cout<<"Hello World";

return 0;
}
*/
/*wrp to print student details//
#include <iostream>
using namespace std;

class Student {
private:
int roll_no;
char name[50];
float marks;

public:
// Function to input student details
void inputData() {
cout << "Enter Roll Number: ";
cin >> roll_no;

cout << "Enter Name: ";


cin>>name;
cout << "Enter Marks: ";
cin >> marks;
}

// Function to display student details


void displayData() {
cout << "\nStudent Details:\n";
cout << "Roll Number: " << roll_no << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
};

int main() {
Student s; // Creating an object of Student class
s.inputData(); // Taking input
s.displayData(); // Displaying output
return 0;
}
*/
/*wrp to to demonstrate theinline function//
#include <iostream>
using namespace std;

class operators{
public:
// Inline function to add two numbers
inline int add(int a, int b) {
return a + b;
}

// Inline function to multiply two numbers


inline int multiply(int a, int b) {
return a * b;
}
};

int main() {
operators obj;

int num1, num2;


cout << "Enter two numbers: ";
cin >> num1 >> num2;

cout << "Addition: " << obj.add(num1, num2) << endl;


cout << "Multiplication: " << obj.multiply(num1, num2) << endl;

return 0;
}*/
/*wrp to demonstrate the static data memebrand static member function//
#include <iostream>
using namespace std;

class Student {
private:
int roll_no;
string name;
static int count; // Static data member (shared by all objects)

public:
// Constructor to initialize student details
Student(string n, int r) {
name = n;
roll_no = r;
count++; // Increment count when a new student object is created
}

// Static function to return total number of students


static int getCount() {
return count;
}

// Function to display student details


void display() {
cout << "Roll No: " << roll_no << ", Name: " << name << endl;
}
};

// Initializing static data member


int Student::count = 0;

int main() {
// Creating static objects
static Student s1("Alice", 101);
static Student s2("Bob", 102);

cout << "Student Details:\n";


s1.display();
s2.display();

// Calling static member function to get the count of objects


cout << "Total Students: " << Student::getCount() << endl;

return 0;
}*/
/*wrp to implements the stuent details//
#include <iostream>
#include <cstring>
using namespace std;

class Student {
private:
int roll_no;
char name[20];
int marks[3];

int total() {
return marks[0] + marks[1] + marks[2];
}

public:

void set_data(int r, const char* n, int m1, int m2, int m3) {
roll_no = r;
strncpy(name, n, 19);
name[19] = ’\0’;
marks[0] = m1;
marks[1] = m2;
marks[2] = m3;
}

// Inline function defined outside the class


inline void display_data();
};

// Definition of inline function outside the class


inline void Student::display_data() {
cout << "\nStudent Details:" << endl;
cout << "Roll No: " << roll_no << endl;
cout << "Name: " << name << endl;
cout << "Total Marks: " << total() << endl; // Calling nested member function
}

int main() {
Student s;
s.set_data(101, "John Doe", 85, 90, 88);
s.display_data();

return 0;
}*/
/*wrp to generate the student id for all the objetsof the class being created//
#include <iostream>
using namespace std;

class Student {
private:
int student_id;
string name;
static int next_id; // Static variable //

public:
// Constructor//
Student(string n) {
name = n;
student_id = next_id;
next_id++;
}

void display() {
cout << "Student ID: " << student_id << ", Name: " << name << endl;
}
};

int Student::next_id = 101;

int main() {
// Creating student objects
Student s1("sriksnth");
Student s2("sanjay");
Student s3("teja");
Student s4("naveen");
Student s5("chethan");

// Displaying student details


s1.display();
s2.display();
s3.display();
s4.display();
s5.display();

return 0;
}*/
/*wrp to enhance car class from previous practical by making data members private//
#include <iostream>
using namespace std;

class Car {
private:
string brand;
string model;
int year;
double price;

public:
// Setter functions to modify private members
void setBrand(string b)
{ brand = b;

}
void setModel(string m) {
model = m;
}
void setYear(int y)
{ year = y;
}
void setPrice(double p)
{ price = p;

// Getter functions to access private members


string getBrand()
{ return brand;
}
string getModel()
{ return model;
}
int getYear()
{ return year;
}
double getPrice()
{ return price;
}

// n to display car details


void displayCar() {
cout << "Car Details:" << endl;
cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
cout << "Price: $" << price << endl;
}
};

int main() {
Car car1;

// car details using setters


car1.setBrand("Toyota");
car1.setModel("tata");
car1.setYear(2022);
car1.setPrice(25000);
// Displaying car details
car1.displayCar();

cout << "\nCar Brand: " << car1.getBrand() << endl;

return 0;
}*/
/*wrp to demonstrate callby value//
#include <iostream>
using namespace std;

void Value(int x) {
x = x + 10;
cout << "(Call by Value): " << x << endl;
}

int main() {
int a = 5;
Value(a);
cout << "(Original Value): " << a << endl;
return 0;
}*/
/* wrp to demnatrsate the call by address//
#include <iostream>
using namespace std;

void Value(int *x) {


*x = *x + 10;
cout << "(Call by Address): " << *x << endl;
}

int main() {
int a = 5;
Value(&a);
cout << " (Original Value): " << a << endl;
return 0;
}*/
/*wrp to demonstrate the call by reference//
#include <iostream>
using namespace std;

void Value(int &x) {


x = x + 10;
cout << "(Call by Reference): " << x << endl;
}

int main() {
int a = 5;
Value(a);
cout << "(Original Value): " << a << endl;
return 0;
}*/
/*wrp to demonstrate this pointer//
#include <iostream>
using namespace std;

class Test {
int x;
public:
void setX(int x) {
this->x = x;
}
void print() {
cout << "Value of x: " << this->x << endl;
}
};

int main() {
Test obj;
obj.setX(10);
obj.print();
return 0;
}*/
/*wrp to demonstrate the friend function//
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int x) : x(x) {}
friend void printX(Test &obj); // Friend function declaration
};

void printX(Test &obj) {


cout << "Value of x: " << obj.x << endl; // Accessing private member
}

int main() {
Test obj(10);
printX(obj);
return 0;
}*/
/*wrp to demonstrate the friend class//
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int x) : x(x) {}
friend class FriendClass; // Friend class declaration
};

class FriendClass {
public:
void printX(Test &obj) {
cout << "Value of x: " << obj.x << endl; // Accessing private member
}
};

int main() {
Test obj(10);
FriendClass fc;
fc.printX(obj);
return 0;
}*/
/*wrp to demonstrate the bank acc to using constructors //
#include <iostream>
using namespace std;

class BankAccount {
int accountNumber;
double balance;
public:
// Default Constructor//
BankAccount() {
accountNumber = 0;
balance = 0.0;
cout << "Default Constructor" << endl;
}

// Parameterized Constructor
BankAccount(int accNum, double bal) {
accountNumber = accNum;
balance = bal;
cout << "Parameterized Constructor Called!" << endl;
}

// Destructor
~BankAccount() {
cout << "Destructor Called for Account Number: " << accountNumber << endl;
}

void display() {
cout << "Account Number: " << accountNumber << ", Balance: " << balance << endl;
}
};

int main() {
BankAccount acc1; // Default Constructor
acc1.display();

BankAccount acc2(66687, 1000.50); // Parameterized Constructor


acc2.display();

return 0;
}*/
/* private defoult constructor//
#include <iostream>
using namespace std;

class Test {
Test() {
cout << "Private Default" << endl;
}
public:
static Test createObject() {
return Test(); // Accessing private constructor inside the class
}
};

int main() {
Test obj = Test::createObject(); // static function//
return 0;
}*/
/* defoult constructor//
#include <iostream>
using namespace std;

class Test {
public:
Test() {
cout << "Default Constructor" << endl;
}
};

int main() {
Test obj; // Default Constructor is called
return 0;
}*/
/*Parameterized Constructor//
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int val) {
x=val;
cout << "Parameterized Constructor Value: " << x << endl;
}
};

int main() {
Test obj(10); // Parameterized Constructor
return 0;
}*/
/*copy constructor//
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int val) : x(val) {}
Test(const Test &obj) { // Copy Constructor
x = obj.x;
cout << "Copy Constructor Value: " << x << endl;
}
void display() {
cout << "Value: " << x << endl;
}
};

int main() {
Test obj1(10);
Test obj2 = obj1; // Copy Constructor
obj2.display();
return 0;
}*/
/* wrp defoult cpy constructor//
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int val) : x(val) {}
void display() {
cout << "Value: " << x << endl;
}
};

int main() {
Test obj1(10);
Test obj2 = obj1; // Default Copy Constructor
obj2.display();
return 0;
}*/
/*dynamkic constructor//
#include <iostream>
using namespace std;

class Test {
int *ptr;
public:
Test(int size) { // Dynamic Constructor
ptr = new int[size];
cout << "Dynamic Constructor" << endl;
}
~Test() { // Destructor
delete[] ptr;
cout << "Destructor" << endl;
}
};

int main() {
Test obj(5); // Dynamic Constructor
return 0;
}*/
/*shollow copy vs deep copy
#include <iostream>
using namespace std;
class Test {
int *ptr;
public:
Test(int val) {
ptr = new int(val);
}
// Deep Copy Constructor
Test(const Test &obj) {
ptr = new int(*obj.ptr);
cout << "Deep Copy constructor" << endl;
}
void setValue(int val) {
*ptr = val;
}
void display() {
cout << "Value: " << *ptr << endl;
}
~Test() {
delete ptr;
}
};

int main() {
Test obj1(10);
Test obj2 = obj1; // Deep Copy Constructor
obj2.setValue(20);
obj1.display();
obj2.display();
return 0;
}*/

You might also like