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

code

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)
3 views

code

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/ 5

Conversion

#include <iostream>
using namespace std;

// Class representing distance in meters


class Distance {
private:
int meters;

public:
// Default ructor
Distance(int m = 0) : meters(m) {}

// Basic to Class Type conversion (float feet to meters)


Distance(float feet) {
meters = static_cast<int>(feet * 0.3048); // Convert feet to me
ters
}

// Class to Basic Type conversion (convert meters to int)


operator int() {
return meters;
}

// Class to Basic Type conversion (convert meters to float)


operator float() {
return static_cast<float>(meters);
}

void display() {
cout << meters << " meters" << endl;
}

int getMeters() {
return meters;
}
};

// Class representing distance in kilometers


class KiloDistance {
private:
float kilometers;

public:
// Default ructor
KiloDistance(float km = 0.0) : kilometers(km) {}
// One Class to Another Class Type conversion (Distance to KiloDist
ance)
KiloDistance( Distance& dist) {
// Convert meters to kilometers
kilometers = dist.getMeters() / 1000.0f;
}

void display() {
cout << kilometers << " kilometers" << endl;
}

float getKilometers() {
return kilometers;
}
};

int main() {
// Basic to Class Type conversion
// Convert float (feet) to Distance (meters)
Distance d1 = 10.0f; // 10 feet converted to meters
cout << "Basic to Class Type Conversion:" << endl;
d1.display(); // Displays meters

// Class to Basic Type conversion


// Convert Distance to int (meters)
int meters = d1; // Implicit conversion to int
cout << "\nClass to Basic Type Conversion:" << endl;
cout << "Distance in meters (int): " << meters << endl;

// Another Class to Basic Type conversion


// Convert Distance to float
float floatMeters = d1;
cout << "Distance in meters (float): " << floatMeters << endl;

// One Class to Another Class Type conversion


// Convert Distance to KiloDistance
cout << "\nOne Class to Another Class Type Conversion:" << endl;
KiloDistance kd = d1; // Convert meters to kilometers
kd.display(); // Displays kilometers

// Data Conversion Example


Distance d2(5000); // 5000 meters
KiloDistance kd2 = d2; // Convert 5000 meters to kilometers

cout << "\nData Conversion Example:" << endl;


cout << "Original Distance: ";
d2.display();
cout << "Converted to Kilometers: ";
kd2.display();
return 0;
}

File Operations
#include <iostream>
#include <fstream>
using namespace std;

int main() {
string filename = "example.txt";

// 6.4 Opening and Closing a File


fstream file; // Create a file stream (for both reading and writing)

// Open file for writing (creating if not exists)


file.open(filename, ios::out); // Open file in output mode
if (file.is_open()) {
file << "Hello, File Processing in C++!" << endl; // Write to f
ile
file << "This is a second line." << endl; // Write another line
file << "Reading characters is fun!" << endl; // Write another
line
cout << "File created and written successfully!" << endl;
file.close(); // Close the file after writing
} else {
cout << "Failed to open the file for writing!" << endl;
}

// 6.4 Reading the file Line by Line


file.open(filename, ios::in); // Open file in input mode for readin
g
if (file.is_open()) {
string line;
cout << "\nReading from the file (Line by Line):" << endl;
while (getline(file, line)) { // Read each line
cout << line << endl; // Display file contents line by line
}
file.close(); // Close the file after reading
} else {
cout << "Failed to open the file for reading!" << endl;
}

// 6.4 Reading the file Character by Character


file.open(filename, ios::in); // Open file in input mode for readin
g
if (file.is_open()) {
char ch;
cout << "\nReading from the file (Character by Character):" <<
endl;
while (file.get(ch)) { // Read each character
cout << ch; // Display the character
}
cout << endl; // Move to the next line after reading all charac
ters
file.close(); // Close the file after reading
} else {
cout << "Failed to open the file for reading!" << endl;
}

// 6.5 Deleting a File


// Open the file in output mode to delete its contents (overwrite)
file.open(filename, ios::out | ios::trunc); // Open in truncate mod
e (clear contents)
if (file.is_open()) {
cout << "\nFile contents deleted!" << endl;
file.close(); // File is truncated (emptied)
} else {
cout << "Failed to open the file for deletion!" << endl;
}

// 6.6 File Modes (Append Mode)


file.open("example2.txt", ios::out | ios::app); // Open file in app
end mode
if (file.is_open()) {
file << "Appending new data!" << endl; // Append data to the fi
le
cout << "Data appended to file successfully!" << endl;
file.close(); // Close the file after appending
} else {
cout << "Failed to open the file in append mode!" << endl;
}

return 0;
}

String Functions
#include <iostream>
#include <string>
using namespace std;

int main() {
string name = "Teja";
string lastname = "Patil";
string fullname = name.append(lastname); // String concatenation
cout << "Full name is: " << fullname << endl;

string nickname = "Tejo";


int n = name.compare(nickname); // String comparison
if (n == 0) {
cout << name << " is equal to " << nickname << endl;
} else if (n < 0) {
cout << name << " is smaller than " << nickname << endl;
} else {
cout << name << " is bigger than " << nickname << endl;
}

// Find the position of a character in the string


int pos = name.find("z"); // Finding position of "Te"
if(pos>-1){
cout << "'Te' found at position: " << pos << endl;
} else {
cout << "'Te' not found in the string!" << endl;
}

// String reversing
string rev_str(name.rbegin(), name.rend()); // Reverse the string
cout << "Reversed string: " << rev_str << endl;

return 0;
}

You might also like