0% found this document useful (0 votes)
2 views9 pages

Programming z

The document contains C++ code examples demonstrating object-oriented programming concepts, including classes for Car and Rectangle with methods for acceleration, braking, area, and perimeter calculations. It also includes a program for collecting user details and saving them to a file, followed by reading the details back from the file. The code showcases basic input/output operations and file handling in C++.

Uploaded by

dnpawlosyohannes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Programming z

The document contains C++ code examples demonstrating object-oriented programming concepts, including classes for Car and Rectangle with methods for acceleration, braking, area, and perimeter calculations. It also includes a program for collecting user details and saving them to a file, followed by reading the details back from the file. The code showcases basic input/output operations and file handling in C++.

Uploaded by

dnpawlosyohannes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

WOLKIT

COLLEGE OF COMPUTING AND


INFORMATICS

DEPARTMENT OF SOFTWARE
ENGINEERING

COURSE COMPUTER
PROGRAMMING LL

NAME:ZELALEM TESFA

ID:NSR/0868/16
SUBMITTED TO MR. EYUEAL

SUBMITTED DATE 16/04/2017 E.C


1.a,

#include <iostream>

#include <string>

using namespace std;

class Car {

private:

string make;

string model;

int year;

int speed;

public:

// Constructor

Car(string carMake, string carModel, int carYear)

: make(carMake), model(carModel), year(carYear), speed(0) {}

// Getter for make

string getMake() const {

return make;

}
// Getter for model

string getModel() const {

return model;

// Getter for year

int getYear() const {

return year;

// Getter for speed

int getSpeed() const {

return speed;

// Method to accelerate

void accelerate() {

speed += 10;

cout << "Accelerating... Current speed: " << speed << " km/h" << endl;

}
// Method to brake

void brake() {

if (speed >= 10) {

speed -= 10;

} else {

speed = 0;

cout << "Braking... Current speed: " << speed << " km/h" << endl;

};

int main() {

Car myCar("Toyota", "Corolla", 2022);

cout << "Car: " << myCar.getMake() << " " << myCar.getModel() << " (" <<
myCar.getYear() << ")" << endl;

myCar.accelerate();

myCar.accelerate();

myCar.brake();

return 0;

}
1.b,

#include <iostream>

using namespace std;

class Rectangle {

private:

double length;

double width;

public:

// Constructor

Rectangle(double rectLength, double rectWidth)

: length(rectLength), width(rectWidth) {}

// Method to calculate the area

double calculateArea() const {

return length * width;

// Method to calculate the perimeter

double calculatePerimeter() const {


return 2 * (length + width);

};

int main() {

Rectangle myRectangle(5.0, 3.0);

cout << "Area of the rectangle: " << myRectangle.calculateArea() << endl;

cout << "Perimeter of the rectangle: " << myRectangle.calculatePerimeter() <<


endl;

return 0;

2,

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

string filename = "user_details.txt";

string name, address;


int id, age;

// Getting user details

cout << "Enter your name: ";

getline(cin, name);

cout << "Enter your ID: ";

cin >> id;

cout << "Enter your age: ";

cin >> age;

cin.ignore(); // Clear newline left in buffer by cin

cout << "Enter your address: ";

getline(cin, address);

// Writing details to a file

ofstream outFile(filename);

if (outFile.is_open()) {

outFile << "Name: " << name << endl;

outFile << "ID: " << id << endl;

outFile << "Age: " << age << endl;


outFile << "Address: " << address << endl;

outFile.close();

cout << "\nYour details have been saved to '" << filename << "' successfully.\
n";

} else {

cerr << "Error: Could not open file for writing.\n";

return 1;

// Reading details back from the file

ifstream inFile(filename);

if (inFile.is_open()) {

cout << "\nReading content from '" << filename << "':\n";

string fileContent;

while (getline(inFile, fileContent)) {

cout << fileContent << endl;

inFile.close();

} else {

cerr << "Error: Could not open file for reading.\n";

return 1;

}
return 0;

You might also like