<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Natalia</title>
    <description>The latest articles on DEV Community by Natalia (@nasz_joy).</description>
    <link>https://dev.to/nasz_joy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F461078%2Fa8ad4ba8-d4fc-4f64-9f0e-7c6481ffc07f.jpg</url>
      <title>DEV Community: Natalia</title>
      <link>https://dev.to/nasz_joy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nasz_joy"/>
    <language>en</language>
    <item>
      <title>How To Get Started With Classes And Objects in C++</title>
      <dc:creator>Natalia</dc:creator>
      <pubDate>Mon, 31 Aug 2020 17:35:23 +0000</pubDate>
      <link>https://dev.to/nasz_joy/how-to-get-started-with-classes-and-objects-in-c-3h3l</link>
      <guid>https://dev.to/nasz_joy/how-to-get-started-with-classes-and-objects-in-c-3h3l</guid>
      <description>&lt;p&gt;For a few days I've been studying classes in C++, here's what I've learned!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Classes&lt;/strong&gt; are &lt;strong&gt;user-defined types&lt;/strong&gt;. It means that the user can create a new &lt;strong&gt;type&lt;/strong&gt; for their desired things. An &lt;strong&gt;Object&lt;/strong&gt; is a &lt;strong&gt;variable&lt;/strong&gt;, that can &lt;strong&gt;store&lt;/strong&gt; those desired things.&lt;/p&gt;

&lt;p&gt;For example, if we want to create a variable that stores numbers, we would create type &lt;strong&gt;int&lt;/strong&gt; (because it stores numbers) and then &lt;strong&gt;variable&lt;/strong&gt; called numbers, so our code would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HoYphh1X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7tt0jdsjyv3ci1py4ua8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HoYphh1X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7tt0jdsjyv3ci1py4ua8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when we have those basics covered let's start with learning about &lt;strong&gt;classes.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  How To Create A Class?
&lt;/h1&gt;

&lt;p&gt;Now, to create a class, we can use two keywords: &lt;strong&gt;class&lt;/strong&gt; or &lt;strong&gt;struct&lt;/strong&gt;. We're going to use the keyword class. To create a class, first, we have to think about what do we want to store there. For example, let's create a class named Home.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wWIYkgSz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/in8hyqhag0p35eo5vibd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wWIYkgSz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/in8hyqhag0p35eo5vibd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now officially created a class! Of course, it does nothing for now, but it &lt;em&gt;is&lt;/em&gt; a class.&lt;/p&gt;

&lt;h1&gt;
  
  
  What's a Class Member?
&lt;/h1&gt;

&lt;p&gt;You can think of it like a real class member. In every class (in school) there are at least a few class members. So, let's go ahead and create them! We're gonna add class member named &lt;strong&gt;household&lt;/strong&gt;. You can add something else if you want i.e. car or even a pet!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KEBkwsjt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8tmcjeu4vtbce5nc38go.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KEBkwsjt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8tmcjeu4vtbce5nc38go.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;int household;&lt;/code&gt; This part of the code is also called an Attribute.&lt;/p&gt;

&lt;p&gt;Now our class Home has its first class member/attribute household! Let's keep going.&lt;/p&gt;

&lt;h1&gt;
  
  
  Access specifiers
&lt;/h1&gt;

&lt;p&gt;Of course classes in our projects won't have only one class member. When we want to create a lot of attributes but we don't want the user to see all of it (i.e. at the ATM we don't want to know our password before manually typing it in), we then use &lt;strong&gt;control operators or access specifiers,&lt;/strong&gt; call it as you like.&lt;/p&gt;

&lt;p&gt;There are three types of access specifiers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;public&lt;/strong&gt; Everyone has access&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;private&lt;/strong&gt; Only the class itself and friends have access&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;protected&lt;/strong&gt; Only the class itself, derived classes and friends have access&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we won't touch the protected specifier but know that it does exist.&lt;/p&gt;

&lt;p&gt;Okay, now we know that we have access specifiers, but how do we use them in our code?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qi5TcnXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jdw5026do3itvowm29p5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qi5TcnXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jdw5026do3itvowm29p5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, all the functions we added after saying &lt;code&gt;public:&lt;/code&gt; are public, meaning, anyone can access that information. Okay, now you should know that the code we add after access specifier is called &lt;strong&gt;method&lt;/strong&gt;. So in our method, we created two variables, num (is type int so it stores numbers) and text (which stores sequents of characters).&lt;/p&gt;

&lt;p&gt;Now let's rewind for a second and visually pictured what we've learned so far:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O04EP30F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/efi2xyykt856i2bctr56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O04EP30F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/efi2xyykt856i2bctr56.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What about Objects?
&lt;/h1&gt;

&lt;p&gt;Well, the title is about class and objects so let's get into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Objects&lt;/strong&gt; are, like the name says, objects, like house, car, bike etc. We can add many objects to our &lt;strong&gt;class.&lt;/strong&gt;They go after the class curly bracket ends.&lt;/p&gt;

&lt;p&gt;To create an object we first, have to say to which class this object belongs to followed by the name of our object:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fGtW6Igz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fzvfvaz1jzyueb0ial2j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fGtW6Igz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fzvfvaz1jzyueb0ial2j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when we want to access the &lt;strong&gt;attributes&lt;/strong&gt; we created before we can do this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fwRHa4Fn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vcch0641jthdhi7hjrbn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fwRHa4Fn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vcch0641jthdhi7hjrbn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if we want to print out our object with its attributes, you can simply use cout for that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---J-SCKgD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ysuq1d3wzdu26sl4j4ib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---J-SCKgD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ysuq1d3wzdu26sl4j4ib.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is it!&lt;/strong&gt; Of course, there are more details to it but for starting out its essential to have the basics covered. This is my first article! So tips, advises etc. are appreciated. Thank you for reading and I hope you found it helpful!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
