Classes in C++ - Reading Assignment

1. Why do we need classes, why can’t we just use structs?

Because usually we want to apply functions that work with a group of data and the way to do that is by creating a class.

2. What are methods?

Methods are functions (actions) which are defined inside a class.

3. How would you define a class?

Example:

class ClassName {

}

4. What is an object?

An object is an instance of a class.

1 Like
  1. because classes hold data and functions, structures only hold data
  2. are functions defined inside of a class

class NewClass
{
int one {};
int two {};
int three {};
};
4. object is a data type

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Essentially, they are the same thing and operate in the same way. However, as a rule of thumb, you want to confine data-only structures to structs and both data and function structures to classes.

  2. What are methods?
    Methods are also known as member functions, which are functions defined inside of a class structure.

  3. How would you define a class?
    class IvanOnTech{
    public:
    //content
    }:

  4. What is an object?
    An object is the product of a class/struct. A class/struct doesn’t actually do anything, it’s just the blueprint of the data structure. To actually create an object, you need to define a variable of the class-type (ie IvanOnTech cpp {x , y};

1 Like

1.) Classes are different than structs in that they also hold data, but provide functions that work with the data as well.
2.) Methods are functions that are defined within the class.
3.) You define a class using the class keyword.
4.) An object is an element of a class.

1 Like
  1. By default, class members are private while struct members are public.
  2. Methods are member functions defined within in a class or struct.
  3. A class is a template used to define certain data types and functions as members within that class.
  4. An object is an instantiation of a class, where it’s members have been initialized and allocated in memory.
1 Like
  1. Classes are very similar to structs and are essentially the same; however, in object-oriented programming we want our types to not only hold data but provide functions that also work well with the data. This is typically done with classes.

  2. Methods are functions that are defined inside of a class.

  3.   class IntPair {
                       int first;
                       int second;
    
                       void print()  {
                             cout <<  "The numbers are" << first << second << endl;
                                     }
                    }
    
  4. An object is a data item that has be initialized. An instance of a class; not when it is defined; but, given a value and allocated a region of memory.

1 Like

1. Why do we need classes, why can’t we just use structs?
We need classes as well as structs so we can have not only data in our types but also so that they can provide functions that work with the data as well.
2. What are methods?
Methods are functions that are defined in the class, also called a member function.
3. How would you define a class?
class myClass{
public:
//variables and functions here
};
4. What is an object?
An object is a variable of a class type. The class is just the blueprint and it needs to be instantiated by objects to hold data.

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Classes can hold member functions inside the class where typically structs only hold data.

  2. What are methods?

Methods are function calls within a class

How would you define a class?
you define a class using member variable and member functions.

  1. What is an object?
    An object is an instance within a class

//Here is the first part of Question #1 at the end of the Chapter. I am to define a class that can hold to variables and two functions. The first function will set the values of the variable and the second function will print the values of both variables. I got it to work with adding the function member to print the value of the variables but to set them did not work for some reason and I had to manually set the values by declaring a class and initializing the variables. However, I will try to get the other part next.

// Question #1 at end of reading assignment on classes

#include <iostream>

using namespace std;
class IntPair
{
public:
    int m_p1{};
    int m_p2{};

void print5()
{cout<<"The value of interger(1) =   "<<m_p1<<endl;
cout<<"The value of interger(2) =   "<<m_p2<<endl;
cout<<"Rewrite the value in this form  " <<"Pair"<<"("<<m_p1<<","<<m_p2<<")"<<endl;

}
};

int main()
{
IntPair sample{5,10}; //initialize integer values for class sample

sample.print5();  //Print integer values for class sample using member function print5();


    return 0;
}
1 Like

You forgot to define a class constructor that initializes the variables, I updated your class with a constructor:

class IntPair
{
public:
    int m_p1{};
    int m_p2{};
    
    IntPair(int p1, int p2) {
        m_p1 = p1;
        m_p2 = p2;
    }

    void print5()
    {cout<<"The value of interger(1) =   "<<m_p1<<endl;
    cout<<"The value of interger(2) =   "<<m_p2<<endl;
    cout<<"Rewrite the value in this form  " <<"Pair"<<"("<<m_p1<<","<<m_p2<<")"<<endl;
    
    }
};

Hey thanks

I did not want to look at the results but what I was trying to do but it didn’t work was to create a member function called set to initialize the variables
int set(int p1, int p2)
{return {m_p1, m_p2};}

and then in the main (){

IntPair p0{set(5,10)}; //to initialize p0 to {5,10}

p0.print5(); //to print the pair : (5,10)

My initialization did not work but this is how I did it for the struct. I called the function to initialize the structure. For structures the function is outside the structure as for the class I created a member function inside the class complex.

This reading assignment did not include how to initialize using a struct. That is a new concept. How would you do it by creating a member function and then use it to initialize the new class?

I also thought of doing it this way when I got to main.

IntPair p0{}; //to initialize p0 to {0.0}

p0.set(5,10); // to re-initialize p0

p0.print5(); //to print the pair : (5,10)

The problem I am having is using a member function
// Well I found the solution and the difference was that when I declare an integer the value that was initialized would be zero(0) for some reason, but when I don’t declare it as an int value I get the correct initialization.

class IntPair
{
public:
    int m_p1{};
    int m_p2{};

void set(int p1, int p2)
{
   m_p1 = p1;   // if I say int m_p1= p1; 
   m_p2 = p2;  // and int m_p2 =  p2; then it initializes to zero() instead of the values that I input at p2.set(5.10)

Can you tell me why is that ?  

I also tried using a return statement to do the initialization this way:
int set(int p1, int p2)
{ return {m_p1, m_p2}
};  // This does not work.  I wonder why?

``

Sure you can use setter functions but using a constructor seems like the most elegant way. You cant set the values as you do in structs without it. If you want to do it like that, then you need to define it.

If you want to set each variable independently you have to create a setter for each var. Also the way you used the setter was wrong, instead of having two as a parameter you instead used a float variable (should use a comma, {5, 10}).

This will only return values back to the function, not set them.

Thanks again

I meant (5,10), that is what I used but because I used int m_p2 = p2 , then it initialized to zero. I am thinking that I did not have to declare m_p2 as an integer (int) since it is already declared an integer in the class statement.

And for the second question I see what you are saying when I return the value of m_p1 and m_p2 they just return the value to the function as m_p1 and m_p2. correct?

Thanks again

I meant (5,10), that is what I used but because I used int m_p2 = p2 , then it initialized to zero. I am thinking that I did not have to declare m_p2 as an integer (int) since it is already declared an integer in the class statement.

And for the second statement I can see what you are saying. The return only returns the value back to the function and not set the value. OK thanks

1 Like
  1. Why do we need classes, why can’t we just use structs?
    Structs only contain data types, but can’t store functions that get used with the data types.

  2. What are methods?
    They are functions defined as part of the class.

  3. How would you define a class?
    class myClass {
    public:
    private:
    }

  4. What is an object?
    An instance of class.

1 Like

I tried to make a construct with and without arguments, but when I print both of them this is the output:

constructing without arguments name is David
age is 20
gender is male
name is default name - no name provided
age is 99
gender is female

It’s mixing up the constructs together, what should I do?

class Person {

    public:

        string name;

        int age;

        string gender;

        Person (string n, int a, string g){

            name = n;

            age = a;

            gender = g;

        }

        Person () {

            cout << " constructing without arguements "; 

            name = "defautl name - no name provided ";

            age = 99;

            gender = "female";

        }

        void printInfo (){

            cout << "name is " << name <<endl;

            cout << "age is " << age <<endl;

            cout << "gender is " << gender <<endl;

        }

};

int main () {

    Person david = Person ("David", 20, "male");

    Person ivan = Person ();

    david.printInfo();

    ivan.printInfo();

    return 0;

}

Ohh, if you initialized a variable inside a function with the same name then it will use the variable in the function and will not exist outside of the scope of the function, that is why the class variable was not set after the function was executed. I didn’t saw this in your code but noticed you mentioned this in the comment. :slight_smile:

You’re using cout in the default constructor (without arguments). That is why it gets printed first in the console, because it gets called before the printInfo function.
You should either print the info for david before initializing ivan class or change the print function to check if the data has been initialize with default values and print the message before showing info. :slight_smile:

Oh okay, thank you!!

  1. Why do we need classes, why can’t we just use structs?
    While structs can hold data types only, classes can hold both data and functions within the definition of the classes themselves.

  2. What are methods?
    Methods are member functions inside classes.

  3. How would you define a class?
    A class is defined by using the class keyword and including member variables and member functions in the declaration.

  4. What is an object?
    An object is an instance of a class.

1 Like
  1. Classes allow for faster processing than structs as the complier will only send a copy of the struct to a function. It also is the proper way to store a function as a member.

  2. They are member functions which can be nested within a class and called upon later in a function.

  3. It is a store all data structure that allows for more seamless use and the storage and recall of data and functions.

  4. It is what is created by utilizing a class. For example if you have an employee class every employee is an object.

1 Like