write code Flashcards
(4 cards)
Write a simple C++ class Person with private data members for name and age and public methods to set and get these values.
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; } };
Define a constructor for a class Book that takes parameters for title, author, and year_published.
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; } };
witting a simple queue in c++
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);