write code Flashcards

(4 cards)

1
Q

Write a simple C++ class Person with private data members for name and age and public methods to set and get these values.

A

include <string></string>

using namespace std;

class Person {
private:
string name;
int age;

public:
void setName(const string& newName) { name = newName; }
string getName() const { return name; }

void setAge(int newAge) { age = newAge; }
int getAge() const { return age; } };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define a constructor for a class Book that takes parameters for title, author, and year_published.

A

include <string></string>

using namespace std;

class Book {
private:
string title;
string author;
int year_published;

public:
// Constructor
Book(const string& t, const string& a, int year)
: title(t), author(a), year_published(year) {}

// Getters
string getTitle() const { return title; }
string getAuthor() const { return author; }
int getYearPublished() const { return year_published; } };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

witting a simple queue in c++

A

include <iostream></iostream>

#include <queue>
using namespace std;</queue>

int main() {
queue<int> myQueue;</int>

// Enqueue elements
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly