Prelab 3
Prelab 3
struct node {
int data;
node* next;
};
int main() {
int n = 0;
cin >> n;
node* head1 = createLinkedList(n);
int m = 0;
cin >> m;
node* head2 = createLinkedList(m);
return 0;
}
bài 2
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *createLinkedList(int n); // The implementation is provided implicitly
return head;
}
void print(node *head)
{
while (head != nullptr)
{
cout << head->data << endl;
head = head->next;
}
}
int main()
{
int n = 0;
cin >> n;
if (n > 0)
{
node *head = createLinkedList(n);
print(head);
}
else
{
cout << "Invalid n" << endl;
}
return 0;
}
OOP
//1
#include<iostream>
using namespace std;
class Course {
private:
int ID;
int numOfStudent;
int numOfTeacher;
int numOfTA;
public:
void getinfo();
void disinfo();
};
void Course::getinfo() {
cout << "ID: ";
cin >> ID;
cout << "Number of Students: ";
cin >> numOfStudent;
cout << "Number of Teachers: ";
cin >> numOfTeacher;
cout << "Number of TAs: ";
cin >> numOfTA;
}
void Course::disinfo()
{
cout<<endl;
cout<< "CourseID = "<< ID << endl;
cout<< "Number of student = " << numOfStudent << endl;
cout<< "Number of teacher = " << numOfTeacher << endl;
cout<< "Number of TA = " << numOfTA<< endl;
}
int main() {
int n;
cin >> n;
public:
Integer(int val) {
this->val = val;
}
int getValue() {
return this->val;
}
~Integer() {}
};
int main() {
cout << "Constructor test" <<endl;
Integer i(10);
Integer i2(-10);
Integer *i3 = new Integer(20);
delete i3;
return 0;
}
// cau3
template<typename T>
class Array {
public:
Array(int size, T initValue);
~Array();
void setAt(int idx, const T& value);
T getAt(int idx);
T& operator[](int idx);
void print();
private:
int size;
T* p;
};
template<typename T>
Array<T>::Array(int size, T initValue) {
this->size = size;
this->p = new T[size];
for (int i = 0; i < size; ++i) {
this->p[i] = initValue;
}
}
template<typename T>
Array<T>::~Array() {
delete[] this->p;
}
template<typename T>
void Array<T>::setAt(int idx, const T& value) {
if (idx < 0 || idx >= this->size) {
throw -1;
}
this->p[idx] = value;
}
template<typename T>
T Array<T>::getAt(int idx) {
if (idx < 0 || idx >= this->size) {
throw -1;
}
return this->p[idx];
}
template<typename T>
T& Array<T>::operator[](int idx) {
if (idx < 0 || idx >= this->size) {
throw -1;
}
return this->p[idx];
}
template<typename T>
void Array<T>::print() {
for (int i = 0; i < this->size; ++i) {
cout << (i > 0 ? " " : "") << this->p[i];
}
}
//cau 4
#include <iostream>
using namespace std;
class ClockType {
public:
ClockType(int hour, int minute, int second); // constructor with parameters
ClockType(); // default constructor
void printTime() const;
private:
int hr;
int min;
int sec;
};
ClockType::ClockType() {
hr = 0;
min = 0;
sec = 0;
}
int main() {
ClockType myClock(5, 12, 40);
myClock.printTime();
ClockType defaultClock;
defaultClock.printTime();
return 0;
}
//// cau 5
#include <iostream>
using namespace std;
class Room {
public:
Room(double length, double breadth, double height); // constructor
double calculateArea(); // tính diện tích
double calculateVolume(); // tính thể tích
private:
double length;
double breadth;
double height;
};
// Constructor implementation
Room::Room(double length, double breadth, double height) {
this->length = length;
this->breadth = breadth;
this->height = height;
}
double Room::calculateArea() {
return this->length * this->breadth;
}
double Room::calculateVolume() {
return this->length * this->breadth * this->height;
}
int main() {
cout << "Constructor test" << endl;
Room r(20, 3, 4);
Room *r2 = new Room(10.5, 5.5, 5.4);