Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Attributes of the Class | Fundamentals of OOP in C++
C++ OOP

Stryg for at vise menuen

book
Attributes of the Class

h

syntax

copy
1234
class Name { public: Type AttributeName; };

Public attributes and methods in a class can be directly accessed using the dot operator (.) on an object of the class.

cpp

main

copy
12345678910111213
class Student { public: int age; }; int main() { Student bob; Student ann; bob.age = 25; ann.age = 33; }
Opgave

Swipe to start coding

You are working with a Student class and need to enhance its structure and usage.

  • Add two additional attributes to the Student class: name (string) and gpa (float).
  • Create an object of the Student class.
  • Assign appropriate values to the attributes name, gpa, and email of the created instance.

Løsning

cpp

solution

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4
Vi beklager, at noget gik galt. Hvad skete der?

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

book
Attributes of the Class

h

syntax

copy
1234
class Name { public: Type AttributeName; };

Public attributes and methods in a class can be directly accessed using the dot operator (.) on an object of the class.

cpp

main

copy
12345678910111213
class Student { public: int age; }; int main() { Student bob; Student ann; bob.age = 25; ann.age = 33; }
Opgave

Swipe to start coding

You are working with a Student class and need to enhance its structure and usage.

  • Add two additional attributes to the Student class: name (string) and gpa (float).
  • Create an object of the Student class.
  • Assign appropriate values to the attributes name, gpa, and email of the created instance.

Løsning

cpp

solution

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt