learning C++

Discussion in 'Programmer's Corner' started by PG1995, Sep 28, 2011.

  1. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Hi

    I wanted to know if I can ask C++ related questions here. I'm trying to learn C++ and can write simple codes. But many a time the compiler give many errors which I'm unable to fix myself. It would be very helpful if someone well versed in C++ can help me with the coding. Thanks.

    Regards
    PG
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    As it's in Programmers Corner I don't see why you can't ask :)

    There are C++ programmers here (myself included) who'll try and point you in the right direction.
     
    PG1995 likes this.
  3. HunterDX77M

    Active Member

    Sep 28, 2011
    104
    2
    We'll do our best to help. If you are just starting out learning the language, might I recommend C++ for Dummies from Wiley Publishing? It sounds juvenile, but is a great resource. Highly recommended.
     
  4. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thanks a lot, Dumbo, Hunter.

    I have a book so I have some basic knowledge of the workings of C++. Anyway, thanks for the recommendation of the book, Hunter.

    Let's start here. The code below is giving me a lot of trouble and I'm at end of my wits. I'm trying to learn the concept of class within a class etc. I use Code:Blocks. It would really nice of you if you can fix it. It needs some edits.

    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <cstdlib>
    4. #include <string>
    5.  
    6. using namespace std;
    7.  
    8. //***********************************************************
    9. class Student
    10. {
    11.     private:
    12.         string name; //John
    13.         int rollNumber;
    14.  
    15.     private:
    16.         class Date
    17.         {
    18.             public:
    19.                 int day;
    20.                 string month; // Jan, Feb
    21.                 int year;
    22.  
    23.                 public:
    24.                     void readDate();
    25.                     Date(int dummyDay, string dummyMonth, int dummyYear) : day(dummyDay),
    26.                     month(dummyMonth), year(dummyYear)
    27.                     { }
    28.         };
    29.  
    30.  
    31.     public:
    32.         void readStudent();
    33.         void printStudent();
    34.  
    35. };
    36. //************************************************************************
    37.  
    38.  
    39. int main()
    40. {
    41.     Student student;
    42.     Date date;
    43.  
    44.     student.readStudent();
    45.     student.printStudent();
    46.  
    47.     system("pause");
    48.     return 0;
    49. }
    50.  
    51. //************************************************************
    52.  
    53. void Student::readStudent()
    54. {
    55.     Student student;
    56.  
    57.     cout << "enter name: "; cin >> name;
    58.     cout << "enter roll no.: "; cin >> rollNumber;
    59.     readDate();
    60.     Date(int day, string month, int year);
    61. }
    62.  
    63. //***********************************************************
    64.  
    65. void Student::printStudent()
    66. {
    67.     cout << "name: " << name << endl;
    68.     cout << "roll number: " << rollNumber << endl;
    69.     cout << "date of birth: " << date.day << "" << date.month << "" << date.year << endl;
    70. }
    71.  
    72. //************************************************************
    73.  
    74. void Student::Date::readDate()
    75. {
    76.     int day; string month; int year;
    77.  
    78.     cout << "enter day: "; cin >> day;
    79.     cout << "enter month: "; cin >> month;
    80.     cout << "enter year: "; cin >> year;
    81. }
    82.  
    Errors List:

    Code ( (Unknown Language)):
    1.  
    2. In function 'int main()':|
    3. 41|error: 'Date' was not declared in this scope|
    4. 41|error: expected ';' before 'date'|
    5. In member function 'void Student::readStudent()':|
    6. 58|error: 'readDate' was not declared in this scope|
    7. 59|error: expected primary-expression before '(' token|
    8. 59|error: expected primary-expression before 'int'|
    9. 59|error: expected primary-expression before 'month'|
    10. 59|error: expected primary-expression before 'int'|
    11. In member function 'void Student::printStudent()':|
    12. 68|error: 'date' was not declared in this scope|
    13. ||=== Build finished: 8 errors, 0 warnings ===|
    14.  
     
    Last edited: Sep 28, 2011
  5. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    right, lets go one step at a time.

    in the function main you have the line
    Code ( (Unknown Language)):
    1. Date date;
    which the compiler objects to. It does this because Date is defined as private within Student and is therefore only accessable from within Student. Try sorting that out may as it may help elsewhere.

    When calling Date in readstudent did you mean date ? Also you don't need to specify the "int" as the variables have already been declared, you are just passing their values.

    A comment I'd make (and it's not right or wrong) is about coding style.

    When declaring a class I'd started the class name with C. For example
    Code ( (Unknown Language)):
    1. class CStudent[code=rich]
    2. That way there's less chance of confusion. Compare the following 2 lines
    3. [code=rich]class CStudent
    4. class Student[/code] start a class definition. Now when you come to declare a variable of each type you have [code=rich]CStudent student1
    5. Student student[/code]That way it's obvious that there's no typo between the upper and lower case "S".
    6.  
    7. Also I'd stay clear of using variable names that could be confused with reserved names. You could try starting class variables with "m" to show that they are member variables.
    8.  
    9. If you are just learning C++ then I'd stay clear of doing things like class definitions within another class. Start with simple class definitions and expand your understanding from there.[/code]
     
    PG1995 likes this.
  6. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    You have to learn about access specifiers,their are 3 access specifiers in C++ 1)Private, 2)Public and 3)Protected.In Student class you have declared the Date class as private which means you can only access Date class from inside Student class or its member functions.So when you define Date inside main function you get these errors
    Inside readStudent() function why you have defined a Student class I have no idea why you did that..

    Inside readStudent() function you have called readDate() function,you can't call a function like that you need an object to call that function as its a member function of Date class.So you get this error
    What is it for?? I have no idea.....if you want to define an object, its not the way.For this you get these errors
    Inside printStudent() you have used object "date",you cant do that it was declared and defined inside main function scope, with out passing it as a parameter to printStudent() function you cant use that date object as it was declared inside main's scope, its name is not visible outside main.So you get this error
    Anyway I have no idea how much you know about C++ but as you asked to edit your code I am doing it,but always go with your own logic.Here I have used reference to pass the objects,you can use pointers but reference looks good and in some case it is better to pass objects with them.As per your logic Date needs constructor call but you can also use default constructors to avoid them...

    Code ( (Unknown Language)):
    1.  
    2. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
    3. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<cstdlib>[/COLOR][/SIZE]
    4. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string>[/COLOR][/SIZE]
    5. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;[/SIZE]
    6. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//***********************************************************[/COLOR][/SIZE]
    7. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student[/SIZE]
    8. [SIZE=2]{[/SIZE]
    9. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:[/SIZE]
    10. [SIZE=2]string name; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//John[/COLOR][/SIZE]
    11. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] rollNumber;[/SIZE]
    12.  
    13.  
    14. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:[/SIZE]
    15. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Date[/SIZE]
    16. [SIZE=2]{[/SIZE]
    17. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:[/SIZE]
    18. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] day;[/SIZE]
    19. [SIZE=2]string month; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Jan, Feb[/COLOR][/SIZE]
    20. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] year;[/SIZE]
    21. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:[/SIZE]
    22. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] readDate();[/SIZE]
    23. [SIZE=2]Date([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dummyDay, string dummyMonth, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dummyYear) : day(dummyDay),[/SIZE]
    24. [SIZE=2]month(dummyMonth), year(dummyYear)[/SIZE]
    25. [SIZE=2]{ }[/SIZE]
    26. [SIZE=2]};[/SIZE]
    27.  
    28. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:[/SIZE]
    29. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] readStudent(Date& date);[/SIZE]
    30. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] printStudent(Date& date);[/SIZE]
    31. [SIZE=2]};[/SIZE]
    32. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//************************************************************************[/COLOR][/SIZE]
    33.  
    34. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()[/SIZE]
    35. [SIZE=2]{[/SIZE]
    36. [SIZE=2]Student student;[/SIZE]
    37. [SIZE=2]Student::Date date(1,[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Jan"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],1989);[/SIZE]
    38. [SIZE=2]student.readStudent(date);[/SIZE]
    39. [SIZE=2]student.printStudent(date);[/SIZE]
    40. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    41. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
    42. [SIZE=2]}[/SIZE]
    43. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//************************************************************[/COLOR][/SIZE]
    44. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::readStudent(Date& date)[/SIZE]
    45. [SIZE=2]{[/SIZE]
    46. [SIZE=2]Student student;[/SIZE]
    47. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter name: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    48. [SIZE=2]cin >> name;[/SIZE]
    49. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter roll no.: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    50. [SIZE=2]cin >> rollNumber;[/SIZE]
    51. [SIZE=2]date.readDate();[/SIZE]
    52. [SIZE=2]}[/SIZE]
    53. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//***********************************************************[/COLOR][/SIZE]
    54. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::printStudent(Date& date)[/SIZE]
    55. [SIZE=2]{[/SIZE]
    56. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"name: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << name << endl;[/SIZE]
    57. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"roll number: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << rollNumber << endl;[/SIZE]
    58. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"date of birth: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << date.day << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" / "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << date.month << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" / "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << date.year << endl;[/SIZE]
    59. [SIZE=2]}[/SIZE]
    60. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//************************************************************[/COLOR][/SIZE]
    61. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::Date::readDate()[/SIZE]
    62. [SIZE=2]{[/SIZE]
    63. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter day: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> day;[/SIZE]
    64. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter month: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> month;[/SIZE]
    65. [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter year: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> year;[/SIZE]
    66. [SIZE=2]}[/SIZE]
    67.  
    Good Luck
     
    PG1995 likes this.
  7. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    For pc work I try to steer clear of C++.
    Visual C# express is much easier to learn and debug.
    Its free and very powerful.
     
  8. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186

    Me too, but not powerful like C/C++.C# use .Net its an intermediate language program i.e.. it will target CLR(.Net runtime).C# programs will only run under platform which can run .Net runtime. In case of embedded system programmers they should learn C/C++ or any language which can be compiled or assembled to native code.

    Anyway I like C# very much, and for fast development and for prototype applications I use it all the time.

    I use VisualStudio C++ Express Edition more than CodeBlock with gcc.But both are good and both are free.
     
  9. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thank you, Dumbo, Debjit.

    @Dumbo: Your suggestions proved very useful. The next time I write a code I will implement your suggested coding style.

    @Debjit: Your edited code proved to be very helpful as it gave me an opportunity to play around with the working code and I learned many things.

    I would request you to have a look on the two codes below. They are both working fine. Please do have a look on the comments. Let me know of any suggestions you have. It would better if you copy those codes in your compiler.

    CODE 1:
    Code ( (Unknown Language)):
    1.  
    2. // learning_nested_classes.cpp
    3. // this program demonstrates how the concept of classes and objects work
    4.  
    5. #include <iostream>
    6. #include <cstdlib>
    7. #include <string>
    8.  
    9. using namespace std;
    10.  
    11. ///////////////////////////////////////////////////////////
    12. class Student
    13. {
    14.     private:
    15.         string name; //John
    16.         int rollNumber;
    17.  
    18.  
    19.     public:
    20.         class Date
    21.         {
    22.                 private:
    23.                     int day;
    24.                     string month; // Jan, Feb
    25.                     int year;
    26.  
    27.                 public:
    28.                     void readDate();
    29.                     void printDate();
    30.  
    31.                     Date()
    32.                     { } //you also need to have the default when there is a created one as the one below
    33.                     Date(int dummyDay, string dummyMonth, int dummyYear) : day(dummyDay),
    34.                     month(dummyMonth), year(dummyYear)
    35.                     { }
    36.         };
    37.  
    38.     public:
    39.         void readStudent();
    40.         /* void readStudent(Date& date); you simple readDate() because date members of Date class       are private */
    41.         void printStudent(Date& date);
    42. };
    43. ///////////////////////////////////////////////////////////
    44.  
    45. Student::Date date;// "::" is called scope resolution operator
    46.  
    47. int main()
    48. {
    49. Student student;
    50. //Student::Date date(1,"Jan",1989); you don't wanna do this because you can input the date
    51. //Student::Date date; made this global
    52. student.readStudent();
    53. student.printStudent(date);
    54. cout << endl << endl;
    55.  
    56. system("pause");
    57. return 0;
    58. }
    59. //---------------------------------------------------------
    60.  
    61. void Student::readStudent()
    62. {
    63. //Student student; you do not need to define any variable
    64. cout << "enter name: "; getline(cin, name);
    65. cout << "enter roll no.: "; cin >> rollNumber;
    66. date.readDate();
    67. }
    68. //---------------------------------------------------------
    69.  
    70. void Student::printStudent(Date& date)
    71. {
    72. cout << "name: " << name << endl;
    73. cout << "roll number: " << rollNumber << endl;
    74. /* cout << "date of birth: " << date.day << "/" << date.month << "/" << date.year << endl;
    75. to execute this you need to make data members of Date class public */
    76. date.printDate(); // if data members of Date class are private, use this
    77. }
    78. //---------------------------------------------------------
    79.  
    80. void Student::Date::readDate()
    81. {
    82. cout << "enter day: "; cin >> day;
    83. cout << "enter month: "; cin >> month;
    84. cout << "enter year: "; cin >> year;
    85. }
    86. //---------------------------------------------------------
    87.  
    88. void Student::Date::printDate()
    89. {
    90.     cout << "date of birth: " << date.day << ":" << date.month << ":" << date.year << endl;
    91. }
    92. //---------------------------------------------------------
    93.  
    CODE 2:
    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <cstdlib>
    4. #include <string>
    5.  
    6. using namespace std;
    7.  
    8. ///////////////////////////////////////////////////////////
    9. class Student
    10. {
    11.     private:
    12.         string name; //John Andrews
    13.         int rollNumber;
    14.  
    15.     public://if you make this private you won't be able to access it thru simple means as far as I know
    16.         class Date
    17.         {
    18.             private:
    19.                 int day;
    20.                 string month; // Jan, Feb
    21.                 int year;
    22.  
    23.             public:
    24.                 void readDate();
    25.                 void printDate();
    26.  
    27.                 //Date()
    28.                 //{ }
    29.                 Date(): day(0), month("Jan"), year(0)// to use this constructor get rid the above
    30.                 { }
    31.                 Date(int dummyDay, string dummyMonth, int dummyYear): day(dummyDay),
    32.                 month(dummyMonth), year(dummyYear)
    33.                 { }
    34.         };
    35.  
    36.     public:
    37.         void readStudent();
    38.         void printStudent();
    39.  
    40. };
    41. ///////////////////////////////////////////////////////////
    42.  
    43. Student::Date date (23, "Jan", 2000);
    44.  
    45. int main()
    46. {
    47.     Student student;
    48.  
    49.     student.readStudent();
    50.     student.printStudent();
    51.  
    52.     cout << endl << endl;
    53.  
    54.     system("pause");
    55.     return 0;
    56. }
    57. //---------------------------------------------------------
    58.  
    59. void Student::readStudent()
    60. {
    61.     cout << "enter name: "; getline(cin, name);
    62.     cout << "enter roll no.: "; cin >> rollNumber;
    63.     date.readDate();
    64. }
    65. //---------------------------------------------------------
    66.  
    67. void Student::printStudent()
    68. {
    69.     cout << "name: " << name << endl;
    70.     cout << "roll number: " << rollNumber << endl;
    71.     date.printDate();
    72. }
    73. //---------------------------------------------------------
    74.  
    75. void Student::Date::readDate()
    76. {
    77.     cout << "enter day: "; cin >> day;
    78.     cout << "enter month: "; cin >> month;
    79.     cout << "enter year: "; cin >> year;
    80. }
    81. //---------------------------------------------------------
    82.  
    83. void Student::Date::printDate()
    84. {
    85.     cout << date.day << ": " << date.month << ": " << date.year << endl;
    86. }
    87. //---------------------------------------------------------
    88.  
     
  10. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Not much to say both are working fine, and as you are starting with C++ its good that you found two different ways to do it. But in real life you would be using reference or pointers and making the Date class as private, and its what for we use OOP(encapsulation).

    In the first code you have used default constructor ,but that will not initialize the Date class data members like (day,month,year) when an object of Data class will be created. Which is bad practice. C++ sets some rules for OOP,you can bypass them but a good programmer will avoid that...in C++ class constructors are for initialization of data members i.e.. it guarantee the programmer that whenever an instance of Data class will be created the data members(day,month,year) will be initialized to avoid any kind of error.

    In second code you used global object for Date class,doing that you bound the functions like
    Code ( (Unknown Language)):
    1.  
    2. student.readStudent();
    3. student.printStudent();
    4.  
    to that date object,any other instance of date object can't be used so its really miss use of OOP.

    But again for the first time its too good,that's how we learn...

    Here is a trick,C++ provides a tool called default arguments in class constructors you can use it,here is an example.
    Code ( (Unknown Language)):
    1. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
    2. #include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>
    3.  
    4. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string>
    5. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;
    6. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mcu
    7. {
    8. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
    9. string id;
    10. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] size;
    11. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cost;
    12. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
    13. [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Constructor with default arguments...
    14. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]mcu(string s_id = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"PIC18F4520"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],
    15. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i_size = 8,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i_cost = 200):id(s_id),size(i_size),cost(i_cost)
    16. {}
    17. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] print()
    18. {
    19. cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Microcontroller Size Cost\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    20. cout<<id<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<size<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<cost<<endl;
    21. }
    22. };
    23.  
    24. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()
    25. {
    26. mcu m;
    27. [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//mcu m("PIC16F877A",8,120); //Uncomment this
    28. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//and comment the above to check the effect
    29. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]m.print();
    30. system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    31. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;
    32. }
    33. [/SIZE]
    One more thing you are using "cstdlib" ,in your code its not needed.

    Good Luck
     
  11. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thank you, Debjit. Actually I also wanted to show that your code helped me to clear me of many of the confusions because I had something beforehand which was working and played with it around. Thank you very much. I would need your help in future so please be there! :)

    Best wishes
    PG
     
    Last edited: Sep 30, 2011
  12. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Hi

    The class Date is a private member class of class Student. That means it would be accessible to the class Student but not to something from outside without first resorting to the main class first etc.

    Although readDate() and printDate() functions are public but I believe now they also won't be accessible because you have 'locked'/'sealed' the main entrance. Correct? Please let me know.

    Is there any simple way (I mean which a beginner like me can use) to use private class Date?

    I have also tried to create a structure structDate inside the class Student. Will it work? So far, it isn't? Is creating a structure inside a class allowed?

    Please help me with the queries above. Thank you.

    Regards
    PG


    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <cstdlib>
    4. #include <string>
    5.  
    6. using namespace std;
    7.  
    8. ///////////////////////////////////////////////////////////
    9. class Student
    10. {
    11.     private:
    12.         string name; //John
    13.         int rollNumber;
    14.  
    15.  
    16.     private://[COLOR=Red][B]trying to make this class private and see what happens?[/B][/COLOR]
    17.         class Date
    18.         {
    19.                 private:
    20.                     int day;
    21.                     string month; // Jan, Feb
    22.                     int year;
    23.  
    24.                 public:
    25.                     void readDate();
    26.                     void printDate();
    27.  
    28.                     Date()
    29.                     { } //you also need to have the default when there is a created one as the one below
    30.                     Date(int dummyDay, string dummyMonth, int dummyYear) : day(dummyDay),
    31.                     month(dummyMonth), year(dummyYear)
    32.                     { }
    33.         };
    34.  
    35.     public:
    36.         void readStudent();
    37.         /* void readStudent(Date& date); you use simple readDate() because date members of Date class   are private */
    38.         void printStudent(Date& date);
    39.  
    40.         struct structDate = {int day; string month; int year;};// [COLOR=Red][B]would it work?[/B]
    41. [/COLOR]};
    42. ///////////////////////////////////////////////////////////
    43.  
    44. Student::Date date;// "::" is called scope resolution operator
    45. // if you don't make it global then you would need to pass the date as argument in member functions of // class Student
    46.  
    47. int main()
    48. {
    49. Student student;
    50. //Student::Date date(1,"Jan",1989); you don't wanna do this because you can input the date
    51. //Student::Date date; made this global
    52. student.readStudent();
    53. student.printStudent(date);
    54. cout << endl << endl;
    55.  
    56. system("pause");
    57. return 0;
    58. }
    59. //---------------------------------------------------------
    60.  
    61. void Student::readStudent()
    62. {
    63. //Student student; you do not need to define any variable
    64. cout << "enter name: "; getline(cin, name);
    65. cout << "enter roll no.: "; cin >> rollNumber;
    66. date.readDate();
    67. }
    68. //---------------------------------------------------------
    69.  
    70. void Student::printStudent(Date& date)
    71. {
    72. cout << "name: " << name << endl;
    73. cout << "roll number: " << rollNumber << endl;
    74. /* cout << "date of birth: " << date.day << "/" << date.month << "/" << date.year << endl;
    75. to execute this you need to make data members of Date class public */
    76. date.printDate(); // if data members of Date class are private, use this
    77. }
    78. //---------------------------------------------------------
    79.  
    80. void Student::Date::readDate()
    81. {
    82. cout << "enter day: "; cin >> day;
    83. cout << "enter month: "; cin >> month;
    84. cout << "enter year: "; cin >> year;
    85. }
    86. //---------------------------------------------------------
    87.  
    88. void Student::Date::printDate()
    89. {
    90.     cout << "date of birth: " << date.day << ":" << date.month << ":" << date.year << endl;
    91. }
    92. //---------------------------------------------------------
    93.  
    Errors:
    Code ( (Unknown Language)):
    1.  
    2. 42|error: expected unqualified-id before '=' token|
    3. 20|error: 'class Student::Date' is private|
    4. 46|error: within this context|
    5. ||=== Build finished: 3 errors, 0 warnings ===|
    6.  
     
  13. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Yes.
    Yes.
    As C is a structural programming language,structures are used in C.C is not an Object Oriented Programming (OOP) Language,so C++ was developed to have every thing of C plus OOP and classes was the tool by which we create object in C++.You can think C as a sub set of C++.So you can create structures inside classes and many times its too usefull.In C++ a structure will behave just like class,just one difference if you create any data member or function inside class it will be private and in case of structure it will be public.To change that you have to use access specifiers.

    Your code didn't worked because your structure declaration is not correct,it should be like this
    Code ( (Unknown Language)):
    1. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
    2. struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] structDate
    3. {
    4. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] day;
    5. string month;
    6. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] year;
    7. };[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// would it work?
    8. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=3][/SIZE]

    And your Date class is private you can't access it,so you can't make a global object of Date.

    May this one is simple...
    Code ( (Unknown Language)):
    1. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
    2. #include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>
    3. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string>
    4. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;
    5. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student
    6. {
    7. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
    8. string name;
    9. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] rollNumber;
    10. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Date
    11. {
    12. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
    13. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] day;
    14. string month;
    15. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] year;
    16.  
    17. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] readDate();
    18. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] printDate();
    19. Date([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dummyDay, string dummyMonth, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dummyYear) : day(dummyDay),
    20. month(dummyMonth), year(dummyYear)
    21. { }
    22. };
    23. Date date;[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Create a private instance of Date and use
    24. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//it inside Student class and its member functions
    25. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:
    26. [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Use Student's constructor to initialize Date's data members
    27. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Student([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dummyDay, string dummyMonth, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dummyYear):date(dummyDay,dummyMonth,dummyYear)
    28. {
    29. }
    30. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] readStudent();
    31. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] printStudent();
    32. };
    33.  
    34. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()
    35. {
    36. Student student(0,[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NULL"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],0);
    37. student.readStudent();
    38. student.printStudent();
    39. cout << endl << endl;
    40. system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    41. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;
    42. }
    43.  
    44. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::readStudent()
    45. {
    46. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter name: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; getline(cin, name);
    47. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter roll no.: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> rollNumber;
    48. date.readDate();
    49. }
    50.  
    51. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::printStudent()
    52. {
    53. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"name: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << name << endl;
    54. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"roll number: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << rollNumber << endl;
    55. date.printDate();
    56. }
    57.  
    58. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::Date::readDate()
    59. {
    60. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter day: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> day;
    61. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter month: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> month;
    62. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter year: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; cin >> year;
    63. }
    64.  
    65. [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Student::Date::printDate()
    66. {
    67. cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"date of birth: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << day << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]":"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] <<month << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]":"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << year << endl;
    68. }
    69. [/SIZE]
    We all are here for that....

    Good Luck
     
  14. TheFox

    Active Member

    Apr 29, 2009
    66
    5
    Infact,
    Code ( (Unknown Language)):
    1.  
    2. struct Alpha
    3. {
    4. int A;
    5. };
    6. class Beta
    7. {
    8. public:
    9. int B;
    10. }
    11.  
    Are the same. Stucts are public by default, and classes are private.
     
Loading...