C ++ learning-class and object (2)

Source: Internet
Author: User

Author: gzshun. Original Works. Reprinted, please indicate the source!
Source:
Http://blog.csdn.net/gzshun

Class)It is the core of C ++ object-oriented programming, and is a tool for implementing abstract types. Class is a data type implemented by abstracting data types. A class is an abstraction of a certain type of objects, and an object is an instance of a certain type.
This article briefly introduces the basic usage of classes and objects, which is simple and the most basic C ++ Foundation.
I. Category
Class definition has two parts: declaration and implementation.
Declaration part: Used to declare data members and member functions (methods) in the class ). -> Tell the user what to do? ".
Implementation part: defines member functions. -> Tell the user how to do it ".


Ii. Class keywords

1. Public
Public members can be accessed anywhere in the program.
2. Private
Private Members can only be accessed by Friends of member functions and classes.
3. Protected
Protects members for inheritance.


3. Class Declaration

Only the simplest class declaration is provided below, because a real class also involves many properties or features.
This experiment was conducted in code: blocks. for standardization, a total of three source files are created: Main. cpp test1.cpp test1.h.
Main. cpp: Main Program
Test1.cpp: class definition in the test1.h header file

Test1.h: Class Declaration

//test1.h#ifndef TEST1_H_INCLUDED#define TEST1_H_INCLUDEDclass CAnimal{public:    int getAge();    void setAge(int age);    void addAge();private:    int mAge;};#endif // TEST1_H_INCLUDED


Iv. Class Definition
When the function body of a class member function is defined outside the class, the scope operator ":" must be used to notify the Compiling System of the class to which the function belongs.

//test1.cpp#include "test1.h"int CAnimal::getAge(){    return mAge;}void CAnimal::addAge(){    mAge++;}void CAnimal::setAge(int age){    mAge = age;}

5. object definition
The concept of the canimal class has been created. Now we need to create an animal as an example. Here we will use a cat and a dog as an example. The following two methods are used to instantiate an object:
1. canimal dog; // creates a dog.
2. canimal * cat = new canimal; // a cat is created dynamically.
The following describes how to use dog and cat:

//main.cpp#include <iostream>#include "test1.h"using namespace std;int main(){    //dog    CAnimal dog;    dog.setAge(12);    cout << "Dog is " << dog.getAge() << " years old." << endl;    cout << "After one year." << endl;    dog.addAge();    cout << "Dog is " << dog.getAge() << " years old." << endl;    //cat    CAnimal *cat = new CAnimal;    cat->setAge(12);    cout << "Cat is " << cat->getAge() << " years old." << endl;    cout << "After one year." << endl;    cat->addAge();    cout << "Cat is " << cat->getAge() << " years old." << endl;    return 0;}

6. program running result

Dog is 12 years old.After one year.Dog is 13 years old.Cat is 12 years old.After one year.Cat is 13 years old.

Differences between class and struct in C ++
In C ++ object-oriented languages, the functions of class and struct are very similar, and struct is also a type.
Differences:
A struct is also a class, but the default method of struct is public, while the default method of class is private.
The following code demonstrates that the following class is equivalent to struct:

Class: Class canimal {// The default value is private int mage; public: int getage (); void setage (INT age); void addage () ;}; <=> struct: struct canimal {// The default value is public int getage (); void setage (INT age); void addage (); Private: int mage ;};

8. Why does the class definition end with a semicolon?
What's the problem? Some people say that I am bored.
In fact, the class definition should not end with a semicolon, but the struct in C ++ should end with a semicolon. The reason is very simple. Because of our usage habits, or situations that need to be taken into account by the compiler, after the definition, we can take an object definition list and end with a semicolon.
Let's look at a few simple examples:

1. Class

class CAnimal{//...}dog, cat;

2. struct

struct CAnimal{//...}dog, cat;

3. int A, B, C; // o (∩ _ ∩) O Haha ~

This blog post is very basic, from simple step by step to complex, in order to cultivate interest and perseverance!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.