C++ II answer_key
C++ II answer_key
)
by fl1rry
2.1 Basics
1.
2.
namespace Integer
{
int sum(float a, float b)
{
return (int)(a)+(int)(b);
}
}
namespace Real
{
float sum(float a, float b)
{
return (a+b);
}
}
3.
#include <iostream>
#include <string>
int main() {
std::string name;
int integerVariable;
double doubleVariable;
std::getline(std::cin, name);
4.
#include <iostream>
int main() {
int number;
return 0;
}
5.
6.
#include <iostream>
#include <cstring>
7.
void increase_counter(int *nr)
{
*nr = 14;
}
void increase_counter(int& nr)
{
nr = 15;
}
2.2 Classes
1.
#include <iostream>
class Point2D
{
public:
Point2D(int, int);
int getX();
int getY();
private:
int x;
int y;
};
Point2D::Point2D(int x, int y)
{
this->x = x;
this->y = y;
}
int Point2D::getX()
{
return this->x;
}
int Point2D::getY()
{
return this->y;
}
int main()
{
int x, y;
std::cin >> x >> y;
Point2D p1 = Point2D(x,y);
std::cout << p1.getX() << " " << p1.getY() << std::endl;
}
2.
#include <iostream>
class Point2D
{
public:
Point2D(int, int);
Point2D(const Point2D&);
int getX();
int getY();
private:
int x;
int y;
};
Point2D::Point2D(int x, int y)
{
this->x = x;
this->y = y;
}
Point2D::Point2D(const Point2D& other)
{
this->x = other.x;
this->y = other.y;
std::cout << "Copy constructor called for point(" << this->x << "," << this->y << ")" << std::endl;
}
int Point2D::getX()
{
return this->x;
}
int Point2D::getY()
{
return this->y;
}
void f(Point2D p)
{
// nothing to be done here
}
int main()
{
int x, y;
std::cin >> x >> y;
Point2D p1 = Point2D(x,y);
Point2D p2 = p1;
f(p2);
}
3.
#include <iostream>
class Point2D
{
public:
Point2D(int, int);
Point2D(const Point2D&);
~Point2D();
int getX();
int getY();
private:
int *x, *y;
};
Point2D::Point2D(int x, int y)
{
this->x = new int(x);
this->y = new int(y);
}
int Point2D::getY()
{
return *y;
}
int main()
{
int x, y;
std::cin >> x >> y;
Point2D p1{x,y};
{
Point2D p2(p1);
}
std::cout << p1.getX() << " " << p1.getY() << std::endl;
}
4.
5.
6.
#include <iostream>
class RationalNumber
{
private:
int numerator;
int denominator;
public:
RationalNumber() : numerator(0), denominator(1) {}
void increment() {
numerator += denominator;
}
int main()
{
int x,y;
std::cin>>x>>y;
RationalNumber r{x,y};
r.print();
r.increment();
r.print();
std::cout<<r.realValue()<<std::endl;
return 0;
}
7.
#include <iostream>
class RationalNumber {
private:
int numerator;
int denominator;
public:
RationalNumber() : numerator(0), denominator(1) {}
return 0;
}
8.
#include<iostream>
#include<string>
class Student
{
private:
std::string name;
float lab_grade;
float exam_grade;
public:
Student(const std::string& n, float l_grade, float e_grade)
: name(n), lab_grade(l_grade), exam_grade(e_grade) {}
int main()
{
std::string name;
float l_grade, e_grade;
std::cin >> name >> l_grade >> e_grade;
Student s{name, l_grade, e_grade};
s.print();
s.add_bonus(0.5);
s.print();
return 0;
}
1.
#include <iostream>
#include <string>
class Product
{
private:
std::string name;
int quantity;
static int totalItems;
static int soldItems;
public:
Product(const std::string& n, int q) : name(n), quantity(q) {
totalItems += quantity;
}
void sell(int q) {
int soldQuantity = (q > quantity) ? quantity : q;
quantity -= soldQuantity;
soldItems += soldQuantity;
totalItems -= soldQuantity;
}
int Product::totalItems = 0;
int Product::soldItems = 0;
int main()
{
Product a{"T-shirt", 15};
Product b{"Skirt", 10};
{
Product c{"Hat", 20};
std::cout << "Items:" << Product::getNumberOfItems() << std::endl; // should print 45
c.sell(10);
std::cout << "Items:" << Product::getNumberOfItems() << std::endl; // should print 35
}
return 0;
}
2.
#include <iostream>
#include <string>
class Product
{
private:
std::string name;
int quantity;
public:
Product(const std::string& n, int q) : name(n), quantity(q) {}
int main()
{
std::string name;
int quantity;
std::cin >> name >> quantity;
print(a);
return 0;
}
3.
#include <iostream>
class IntList
{
private:
static const int MAX_SIZE = 100; // Maximum number of elements in the list
int elements[MAX_SIZE];
int count;
public:
IntList() : count(0) {}
void reverse() {
int start = 0;
int end = count - 1;
while (start < end) {
int temp = elements[start];
elements[start] = elements[end];
elements[end] = temp;
start++;
end--;
}
}
a.append(3);
a.append(2);
print(a); // should print [3,2]
b.append(4);
b.append(6);
a.append(b);
print(a); // should print [3,2,4,6]
std::cout << "Lenght is " << a.length() << std::endl; // should print "Length is 4"
a.reverse();
print(a); // should print [6,4,2,3]
return 0;
}
4.
#include <iostream>
#include <vector>
class IntList
{
private:
std::vector<int> elements;
public:
void append(int element) {
elements.push_back(element);
}
int main()
{
IntList l;
l.append(1);
l.append(2);
l.append(4);
std::cout << l << std::endl; // should print [1,2,4]
std::cout << l[0] << std::endl; // should print 1
l[-1] = 3;
std::cout << l << std::endl; // should print [1,2,3]
l = l * 2;
std::cout << l << std::endl; // should print [1,2,3,1,2,3]
l = l * 2;
std::cout << l << std::endl; // should print [1,2,3,1,2,3,1,2,3,1,2,3]
return 0;
}
1.
#include <iostream>
#include <string>
class FormattedPrint
{
public:
static void center(const std::string& text) {
int spaces = (30 - text.length()) / 2;
std::cout << std::string(spaces, ' ') << text << std::endl;
}
class Book
{
private:
std::string title;
std::string author;
std::string description;
public:
Book(const std::string& title, const std::string& author, const std::string& description)
: title(title), author(author), description(description) {}
void print() const {
FormattedPrint::center(title);
FormattedPrint::right(author);
FormattedPrint::left(description);
}
};
//implementation here
int main()
{
std::string title, author, description;
std::getline(std::cin, title);
std::getline(std::cin, author);
std::getline(std::cin, description);
return 0;
}
2.
#include <iostream>
#include <string>
class Address
{
private:
std::string streetName;
int number;
std::string city;
public:
Address() = default;
class Person
{
private:
std::string name;
Address address;
public:
Person(const std::string& name) : name(name), address() {}
void setAddress(const Address& address) {
this->address = address;
}
int main()
{
Person p{"John"};
p.print();
return 0;
}
3.
#include<iostream>
#include<math.h>
class Point2D
{
private:
float x;
float y;
public:
Point2D(float x, float y) : x(x), y(y) {}
class Line2D
{
private:
Point2D point1;
Point2D point2;
public:
Line2D(float x1, float y1, float x2, float y2)
: point1(x1, y1), point2(x2, y2) {}
float length()
{
return point1.distanceTo(point2);
}
};
//implementation here
int main()
{
float x1, y1, x2, y2;
std::cin >> x1 >> y1 >> x2 >> y2;
return 0;
}
4.
#include<iostream>
#include<string>
class Item
{
private:
std::string name;
int price;
public:
Item(const std::string& name, int price)
: name(name), price(price) {}
int main()
{
Item* items[100];
int numberOfItems;
int totalValue = 0;
for (int i = 0; i < numberOfItems; ++i)
{
totalValue += items[i]->getPrice();
delete items[i]; // Free the memory allocated for each item
}
5.
#include<iostream>
#include<string>
#include<vector>
class Engine
{
private:
std::string fuelType;
int horsePower;
public:
Engine(const std::string& fuelType, int horsePower) : fuelType(fuelType),
horsePower(horsePower) {}
class Car
{
private:
std::string name;
std::string fuelType;
int horsePower;
public:
Car(const std::string& name, const std::string& fuelType, int horsePower) : name(name),
fuelType(fuelType), horsePower(horsePower) {}
class AutoPark
{
private:
std::string name;
std::vector<Car> cars;
public:
AutoPark(const std::string& name) : name(name) {}
int main()
{
Car c1{"VW id4", "electrical", 201};
Car c2{"Toyota", "hybrid", 219};
Car c3{"Ferrari", "gasoline", 424};
AutoPark a{"AutomobileRO"};
a.addCar(c1);
a.addCar(c2);
a.addCar(c3);
a.print();
return 0;
}
1.
#include <iostream>
#include <string>
class Person
{
protected:
std::string name;
int age;
public:
Person(const std::string& name, int age) : name(name), age(age) {}
public:
Student(const std::string& name, int age, float grade) : Person(name, age), grade(grade) {}
int main()
{
std::string name;
int age;
float grade;
return 0;
}
2.
#include <iostream>
#include <string>
class Animal
{
protected:
std::string name;
public:
Animal(const std::string& name) : name(name) {}
//implementation here
int main()
{
Animal* a;
Animal* b;
Animal* c;
delete a;
delete b;
delete c;
return 0;
}
3.
#include <iostream>
#include <string>
class Transaction {
public:
virtual ~Transaction() {}
virtual void print() const = 0;
virtual int getValue() const = 0;
};
public:
TopUp(int amount) : amount(amount) {}
public:
CardPayment(int amount, const std::string& shopName) : amount(amount),
shopName(shopName) {}
public:
ATMWithdraw(int amount, unsigned long atmID) : amount(amount), atmID(atmID) {}
class BankAccount {
private:
std::string name;
Transaction* transactions[100];
int numTransactions;
int balance;
public:
BankAccount(const std::string& name) : name(name), numTransactions(0), balance(0) {}
int main()
{
BankAccount b{"Shopping account"};
unsigned int t, c, a;
std::string shopName;
unsigned long atmID;
std::cin >> t;
TopUp topUpTransaction{t};
b.addTransaction(&topUpTransaction);
b.printReport();
std::cout << "Balance: " << b.getBalance() << " RON" << std::endl;
return 0;
}
4.
#include <iostream>
#include <string>
class HandWatch
{
int weight;
public:
HandWatch(int weight) : weight(weight) {}
class ElectronicDevice
{
protected:
std::string name;
int price;
public:
ElectronicDevice(const std::string& name, int price) : name(name), price(price) {}
int main()
{
int weight, price;
std::string name;
s.print();
return 0;
}
5.
#include <iostream>
#include <string>
class Person
{
private:
std::string name;
public:
Person(const std::string& name) : name(name) {
std::cout << "Constructor for Person " << name << std::endl;
}
};
public:
Student(const std::string& name, const std::string& university)
: Person(name), university(university) {
std::cout << "Constructor for Student " << name << " at " << university << std::endl;
}
};
public:
Teacher(const std::string& name, int salary)
: Person(name), salary(salary) {
std::cout << "Constructor for Teacher " << name << " with " << salary << std::endl;
}
};
class PhdStudent : public Student, public Teacher {
public:
PhdStudent(const std::string& name, const std::string& university, int salary)
: Person(name), Student(name, university), Teacher(name, salary) {
std::cout << "Constructor for PhdStudent " << name << " at " << university << " with " <<
salary << std::endl;
}
};
1.
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
class Student
{
private:
std::string name;
std::vector<int> grades;
public:
Student(const std::string& name) : name(name) {}
float getAverage() {
if (grades.empty()) {
throw std::runtime_error("There are no grades");
}
int sum = 0;
for (int grade : grades) {
sum += grade;
}
return static_cast<float>(sum) / grades.size();
}
};
//implementation here
int main()
{
std::string name;
std::cin >> name;
Student student(name);
int numGrades;
std::cin >> numGrades;
try {
student.addGrade(grade);
} catch (const std::invalid_argument& e) {
std::cout << e.what() << std::endl;
}
}
try {
float average = student.getAverage();
std::cout << "The average is: " << average << std::endl;
} catch (const std::runtime_error& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
2.
#include <iostream>
#include <string>
if (b < lowest) {
lowest = b;
}
if (c < lowest) {
lowest = c;
}
std::cout << "The lowest is: " << lowest << std::endl;
}
int main()
{
int i1, i2, i3;
float f1, f2, f3;
std::string s1, s2, s3;
return 0;
3.
#include <iostream>
#include <string>
if (b < lowest) {
lowest = b;
}
if (c < lowest) {
lowest = c;
}
std::cout << "The lowest is: " << lowest << std::endl;
}
class Building {
private:
std::string name;
int numberOfFloors;
public:
Building(const std::string& name, int numberOfFloors)
: name(name), numberOfFloors(numberOfFloors) {}
int main()
{
std::string name;
int numberOfFloors;
return 0;
}
4.
#include <iostream>
#include <string>
#include <vector>
class ComicBook
{
private:
std::string title;
int number;
public:
ComicBook(const std::string& title, int number)
: title(title), number(number) {}
class Collection
{
private:
std::string name;
std::vector<ComicBook> comicBooks;
public:
Collection(const std::string& name)
: name(name) {}
int main()
{
std::string name;
int numItems;
Collection collection(name);
for (int i = 0; i < numItems; i++) {
std::string title;
int number;
std::cin >> title >> number;
ComicBook comicBook(title, number);
try {
collection.addItem(comicBook);
} catch (const std::runtime_error& e) {
std::cout << e.what() << std::endl;
}
}
collection.print();
return 0;
}
5.
#include <iostream>
#include <string>
#include <vector>
public:
Collection(const std::string& name)
: name(name) {}
class Coin {
private:
double value;
std::string currency;
int year;
public:
Coin(double value, const std::string& currency, int year)
: value(value), currency(currency), year(year) {}
bool operator==(const Coin& other) const {
return value == other.value && currency == other.currency && year == other.year;
}
int main() {
std::string name;
int numCoins;
Collection<Coin> coinCollection(name);
return 0;
}
6. #include <iostream>
#include <vector>
int main() {
int N;
std::cin >> N;
std::vector<int> numbers(N);
return 0;
}